Bash Basics

Essential Bash scripting fundamentals

Shebang and Execution

#!/bin/bash # shebang for bash script
chmod +x script.sh # make script executable
./script.sh # run script
bash script.sh # run with bash interpreter

Comments

# This is a comment # single line comment
: <
# multi-line comment
COMMENT

Echo and Print

echo Hello World # print text
echo -n text # print without newline
echo -e text\n # enable escape sequences
printf format args # formatted print

Exit Status

exit 0 # exit with success
exit 1 # exit with error
$? # last command exit status