03 - Shebang
Table of contents
Shebang
- The shebang (
#!
) is a special character sequence at the very beginning of a script file that tells the operating system which interpreter should be used to execute the script.
Syntax
#!/path/to/interpreter
#
: The comment character.!
: Indicates the start of the shebang./path/to/interpreter
: The full path to the interpreter binary (e.g.,/bin/bash
,/usr/bin/python
).
Bash Script (bash
)
#!/bin/bash
- The script uses the Bash interpreter (
/bin/bash
).
Python Script (py
)
#!/usr/bin/python3
- The script uses the Python 3 interpreter (
/usr/bin/python3
).
Shell Script (sh
)
#!/bin/sh
- The script uses the
sh
shell interpreter (/bin/sh
).
Key Points
Mandatory for Direct Execution:
A script with a shebang can be executed directly (e.g.,
./script.sh
) if it has executable permissions:chmod +x script.sh ./script.sh
Default Behavior Without Shebang:
- If no shebang is present, the script is executed by the user's current shell (e.g.,
/bin/bash
or/bin/sh
by default).
- If no shebang is present, the script is executed by the user's current shell (e.g.,
Common Interpreter Paths:
/bin/bash
: Bash shell./bin/sh
: Bourne shell or its compatible version./usr/bin/python3
: Python 3./usr/bin/env
: Finds the interpreter in the environment.
Exit Codes
- In shell scripting, exit codes (also known as return codes or exit status) indicate the success or failure of a command or script.
Exit Code Conventions
Success (0):
- An exit code of
0
means the command or script executed successfully without errors.
- An exit code of
Failure (Non-Zero):
Any non-zero exit code indicates an error or a specific failure condition.
Common non-zero codes are typically integers from
1
to255
.
Checking Exit Codes
Using
$?
:The special variable
$?
holds the exit code of the last executed command.ls /valid/path echo "Exit code: $?" # Output: 0 ls /invalid/path echo "Exit code: $?" # Output: 2 (or another non-zero code depending on the error)
Using
exit
:The
exit
command is used to manually set an exit code in a script.bashCopy code#!/bin/bash echo "This is a script." exit 1 # Sets the exit code to 1
Common Exit Codes
Exit Code | Description |
0 | Success |
1 | General error |
2 | Misuse of shell built-ins |
126 | Command invoked cannot execute (Permission Denied |
127 | Command not found |
128 | Invalid exit argument |
130 | Script terminated by Ctrl+C (SIGINT) |
255 | Exit status out of range |
Functions
Function in a shell script is a piece of or block of reusable code that perform specific task.
They help to organize code, improve readability, and avoid repetition by encapsulating logic into a single callable unit.
When a shell script runs it runs line-by-line. So your Function must always be defined first before calling it, if not then it will give error.
The return statement within a function call helps in specifying the exit code for that function. It is just like the exit code for the entire script but in this case it wont exit the script but the function.
Syntax of Functions
There are two common ways to define a function in shell scripting:
Using the
function
Keyword:function function_name() { # Commands }
Without the
function
Keyword:function_name() { # Commands }
Passing Arguments to a Function
Function arguments are accessed using
$1
,$2
, ...,$n
, where$1
is the first argument,$2
is the second, and so on.$@
or$*
can be used to access all arguments.#!/bin/bash sum() { echo "Sum: $(($1 + $2))" } sum 5 10 # Calls the function with arguments 5 and 10
Returning Values from Functions
Using
echo
: The output ofecho
can be captured.Using
return
: Return a status code (limited to integers between 0 and 255).
Returning Values with echo
:
You can retrieve the return value by assigning the function call to a variable, if using
echo
command.#!/bin/bash square() { echo $(($1 * $1)) } result=$(square 4) # Capture the output of the function echo "Square: $result"
Returning Status Codes with return
:
return
statements are used to return exit status only.It can only returns the numeric value and not the string/text.
You cannot retrieve the return value by assigning the function call to a variable, if using
return
statement, instead use$?
.#!/bin/bash is_even() { if [ $(($1 % 2)) -eq 0 ]; then return 0 # Success else return 1 # Failure fi } is_even 4 # function call if [ $? -eq 0 ]; then echo "The number is even." else echo "The number is odd." fi
ShellCheck
To analyze and identify bugs and area of improvement in your script use
shellcheck
utility.apt-get install shellcheck OR yum install shellcheck