Bash - Script

1 - About

File extensions are meaningless in UNIX, unlike DOS, where EXE, COM, and BAT indicate executable files. Linux use a shebang to define the type of language.

The .sh extension denote shell script files, but doesn't make the script executable. See file permission

3 - Management

3.1 - Start a script

3.1.1 - In another process

Unlike DOS, UNIX does not automatically look in the current directory for a file to execute.

You have to specify:

  • the full file name


  • or the dot (.) shortcut that refers to the current directory

cd /usr/local/scripts/
$  ./

Unix/Linux search for executables only in directories identified in the PATH variable.

3.1.2 - In the same process

If you want to get the variable back, you need to run the script in the same process. For this, you use:

3.1.3 - Run and terminate (exec)

3.1.4 - Run a script in the background

  • nohup is a POSIX command to ignore the HUP (hangup) signal, enabling the command to keep running after the user who issues the command has logged out.
  • By adding the ampersand (&) to the end of the command line, you start the application in background.

3.1.5 - Run a script hosted on the internet

bash <(curl -f -L -sS args


bash <(curl -f -L -sS --help

3.2 - Exit Value

Bash’s exit status is the exit status of the last command executed in the file script. If no commands are executed, the exit status is 0.

3.3 - Timing a script

$SECONDS: The number of seconds the script has been running.



echo "Hit Control-C to exit before $TIME_LIMIT seconds."

while [ "$SECONDS" -le "$TIME_LIMIT" ]
do   #   $SECONDS is an internal shell variable.
  if [ "$SECONDS" -eq 1 ]

  echo "This script has been running $SECONDS $units."
  #  On a slow or overburdened machine, the script may skip a count
  #+ every once in a while.
  sleep $INTERVAL

echo -e "\a"  # Beep!

exit 0

3.4 - Arguments

3.5 - Get the script Name

The special parameter 0 Expands to the name of the shell or shell script. This is set at shell initialization. If bash is invoked with a file of commands, $0 is set to the name of that file.

If bash is started with the -c option, then $0 is set to the first argument after the string to be executed, if one is present. Otherwise, it is set to the file name used to invoke bash, as given by argument zero.

# echo $0


3.6 - Get the script directory

  • for a file

echo $(dirname $0)
# saw also
echo $( cd $(dirname $0) ; pwd -P )

echo $( dirname $(realpath "$0") )

3.7 - Absolute path name

See the underscore special parameter $_

# $() run a subshell therefore there the cd command has no effect
SCRIPT_PATH=$( cd $(dirname $0) ; pwd -P )

3.8 - Checking syntax

With the -n or -o noexec option of the set command, the command are not executed and you can then check a shell script for syntax errors.

4 - Configuration

4.1 - $BASH_ENV

An environmental variable pointing to a Bash startup file to be read when a script is invoked

5 - Documentation / Reference

Data Science
Data Analysis
Data Science
Linear Algebra Mathematics

Powered by ComboStrap