Skip to content
/ run Public

๐Ÿš€ run command && echo $STDOUT

License

Notifications You must be signed in to change notification settings

specs-sh/run

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

68 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Mac (BASH 3.2) BASH 4.0 BASH 4.4 BASH 5.0


๐Ÿš€ run $command

Simplest possible run command for beautiful shell script tests!


  • Simple interface for running commands or functions
  • Stores return or exit code in easy-to-use variable
  • Stores standard output and error output in easy-to-use variables
  • Runs command in either current shell or runs in a subshell

Download the latest version or install via:

curl -o- https://run.specs.sh/install.sh | bash

Source the downloaded run.sh script to use in your tests:

source "run.sh"

run echo "Hello, world"

echo "$STDOUT"
# => "Hello, world"

echo "$STDERR"
# => ""

echo "$EXITCODE"
# => "0"

run $command

$STDOUT

To get the standard output, check the $STDOUT variable:

run echo "Hello, world"

echo "$STDOUT"
# => Hello, world

$STDERR

To get the standard output, check the $STDERR variable:

run foo "This command does not exist"

echo "$STDERR"
# => foo: command not found

$EXITCODE

To get the standard output, check the $EXITCODE variable:

run echo "Hello, world"

echo $EXITCODE
# => 0
run foo "This command does not exist"

echo $EXITCODE
# => 127

{ Current Shell } or {{ Subshell }}

To run a function or command in the current shell:

  • Call normally (no curly braces / no brackets)
    run echo "Hello world" # <--- runs in current shell

Alternatively, to run a function or command in the current shell:

  • Call and wrap with a single curly braces or single brackets
    run { echo "Hello world" } # <--- runs in current shell
    run [ echo "Hello world" ] # <--- runs in current shell 

To run a function or command in a subshell:

  • Call and wrap with double curly braces or double brackets:
    run {{ echo "Hello world" }} # <--- runs in a SUBSHELL
    run [[ echo "Hello world" ]] # <--- runs in a SUBSHELL

โ„น๏ธ Subshells

Commands run inside subshells have access to all local variables but changes are not reflected in the outer shell:

  • setX() { x="$1"; }
    x=5
    
    run {{ setX 42 }} # <--- run in subshell
    echo "$x"
    # => 5            # <--- value of 'x' is still 5
    
    run setX 42       # <--- run in current shell
    echo "$x"
    # => 42           # <--- value of 'x' changed to '42'

run supports both braces and brackets to support commands which use brackets or braces as arguments.

To call the [ shell builtin function, you must wrap the command in braces: run { [ 1 -eq 2 ] }