Stupid Bash Trick: Make StackScripts work from a shell


Some of the functions I wrote up in my StackScripts were so cool, I wanted to use them after the machine was installed. However, on a StackScript where there are tags, it choked up bash.

So, we include the tag in a quote that escapes the <> chars, then wrap the whole thing in a case statement that detects whether the script is being run from an interactive shell, or from a non-interactive one (first boot as a StackScript invokation):

case "$-" in
                PATH=$(cd ${0%/*} && pwd -P)
                source "${PATH}/"
                source "${PATH}/"
                source '<ssinclude stackscriptid="154">'
                source '<ssinclude stackscriptid="162">'

The only thing you have to do after using this code is to make sure you export your UDF variables before you run anything.

2 Replies

You can use the tty command in a script to test if it's being run in an interactive shell. Just do tty > /dev/null and check the exit status:
> 0

Standard input is a terminal.


Standard input is not a terminal.


An error occurred.

Or use the "-t" test

if [ -t 0 ]
  echo STDIN is connected to a terminal
  echo No terminal


Please enter an answer

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct