Created
November 22, 2018 18:34
-
-
Save aks/65b577c1ef619790e6d88d788c561177 to your computer and use it in GitHub Desktop.
Exploration of bash interactivity, SHLVL and `-t 1` values in various forms of invocation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| case "$-" in | |
| *i*) echo 'interactive' ;; | |
| *) echo 'non-interactive' ;; | |
| esac | |
| echo "SHLVL = $SHLVL" | |
| [[ -t 1 ]] && echo "tty STDOUT? : true" || echo "tty STDOUT? : false" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $ ./tstl.sh | |
| Sourcing a script | |
| source ./tst.sh : | |
| non-interactive | |
| SHLVL = 2 | |
| tty STDOUT? : true | |
| Invoking a script | |
| ./tst.sh : | |
| non-interactive | |
| SHLVL = 3 | |
| tty STDOUT? : true | |
| Invoking a script within a string | |
| echo `./tst.sh` : | |
| non-interactive SHLVL = 3 tty STDOUT? : false | |
| Sourcing a script within a subshell | |
| ( source ./tst.sh ) : | |
| non-interactive | |
| SHLVL = 2 | |
| tty STDOUT? : true | |
| Invoking a script within a subshell | |
| ( ./tst.sh ) : | |
| non-interactive | |
| SHLVL = 3 | |
| tty STDOUT? : true | |
| Invoking a script within a string within a subshell | |
| ( echo `./tst.sh` ) : | |
| non-interactive SHLVL = 3 tty STDOUT? : false | |
| Evaluating a script invocation within a string | |
| eval "echo \`./tst.sh\`" : | |
| non-interactive SHLVL = 3 tty STDOUT? : false | |
| Evaluating a script invocation within a string, in a subshell | |
| ( eval "echo \`./tst.sh\`" ) : | |
| non-interactive SHLVL = 3 tty STDOUT? : false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| prog=./tst.sh | |
| echo "Sourcing a script" | |
| echo "source $prog :" | |
| source $prog | |
| echo '' | |
| echo "Invoking a script" | |
| echo "$prog :" | |
| $prog | |
| echo '' | |
| echo "Invoking a script within a string" | |
| echo "echo \`$prog\` :" | |
| echo `$prog` | |
| echo '' | |
| echo "Sourcing a script within a subshell" | |
| echo "( source $prog ) :" | |
| ( source $prog ) | |
| echo '' | |
| echo "Invoking a script within a subshell" | |
| echo "( $prog ) :" | |
| ( $prog ) | |
| echo '' | |
| echo "Invoking a script within a string within a subshell" | |
| echo "( echo \`$prog\` ) :" | |
| ( echo `$prog` ) | |
| echo '' | |
| echo "Evaluating a script invocation within a string" | |
| echo "eval \"echo \\\`$prog\\\`\" : " | |
| eval "echo \`$prog\`" | |
| echo '' | |
| echo "Evaluating a script invocation within a string, in a subshell" | |
| echo "( eval \"echo \\\`$prog\\\`\" ) :" | |
| ( eval "echo \`$prog\`" ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment