0% found this document useful (0 votes)
5 views4 pages

Bash Scripting Essentials Guide

The document provides a comprehensive overview of shell scripting concepts, including redirections, variables, operators, quoted strings, expansion, execution, command line arguments, control structures, interactivity, and process control. It details various commands and their functionalities, along with examples for better understanding. The author of the document is Adolfo Sanz De Diego, and it is licensed under CC-BY-SA.

Translated by

ScribdTranslations
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Bash Scripting Essentials Guide

The document provides a comprehensive overview of shell scripting concepts, including redirections, variables, operators, quoted strings, expansion, execution, command line arguments, control structures, interactivity, and process control. It details various commands and their functionalities, along with examples for better understanding. The author of the document is Adolfo Sanz De Diego, and it is licensed under CC-BY-SA.

Translated by

ScribdTranslations
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

REDIRECTIONS

standard output
make file output to file and to screen
file output a file
file output at the end of the file
/dev/null # descarta output
error
2>&1 # error a output
file file error
file error at the end of the file
2> /dev/null discard error
output and error
2>&1 | tee file both to file and to screen
>& file both to file
&>> file both at the end of the file
VARIABLES
environment variables
$PWD current working directory
$OLDPWD previous working directory
$PPID parent process identifier
$HOSTNAME computer name
$USER username
$HOME user directory
$PATH # command search routes
$LANG language for messages
$FUNCNAME function name in execution
$LINENO current line number (of the script)
$RANDOM random number
special variables
$0 script name
${N} parameter N
$$ current process identifier
$! identifier of the last process
$@ all the received parameters
$# number of parameters received
$? # (0=normal, >0=error) return code of the last command
shift # $1=$2, $2=$3, ... ${N-1}=${N}
ARRAYS
declare -a ARRAY array declaration
ARRAY=(value1 ... valueN) composite assignment
ARRAY[N]=valueN simple assignment
ARRAY=([N]=valorN valorM[P]=valorP) # assign cells N, M and P
${ARRAY[N]} cell value N
${ARRAY[*]} all the values

Autor: Adolfo Sanz De Diego [Link] License:CC-BY-SA


OPERATORS
arithmetic operators
+ sum
- remains
* multiplication
/ division
% restaurant

++ increase
-- decrease
numerical comparison operators
number1–eq number2 number1 equal to number2
number1–number2 number1 different from number2
number1 - lt number2 number1 less than number2
number1 - the number2 number1 less than or equal to number2
number1 -gt number2 number1 greater than number2
number1–the number2 number1 greater than or equal to number2

logical operators
! NOT
&& , -a AND
|| , -o OR
file operators
-e file exists
-s file is not empty
-f file normal
-d file directory
-h file symbolic link
-r file reading permission
-w file write permission
-x file execution permit
The file owner
-G file belongs to the group
f1-ef f2 f1 and f2 links same file
f1-nt f2 f1 newer than f2
f1-ot f2 f1 older than f2
string operators
-n string not empty
-z chain empty
string1 = string2 string1 equals string2
string1 == string2 string1 equals string2
string1 != string2 string1 different from string2

Autor: Adolfo Sanz De Diego [Link] License:CC-BY-SA


QUOTED
#!RUTA path to the interpreter (/bin/bash)
character literal value of the character
linea1 \linea2 to write in several lines
chain literal string value
chain literal string value, except $ ' \
EXPANSION
[prefix]{string1,[,...],stringN}[suffix] # = precad1suf ... precadNsuf
${VARIABLE:-value} if VARIABLE is null, return value
${VARIABLE:=value} if VARIABLE null, assign value
${VARIABLE:?message} if VARIABLE null, error message and end
${VARIABLE:start} trim from start to end
${VARIABLE:start:length} crop from the beginning to length
${!prefix*} variable names with prefix
${#VARIABLE} number of characters of VARIABLE
${#ARRAY[*]} array elements
${VARIABLE#pattern} remove minimum pattern from the beginning
${VARIABLE##pattern} remove maximum pattern from beginning
${VARIABLE%pattern} remove minimum pattern from end
${VARIABLE%%pattern} remove maximum pattern from the end
${VARIABLE/pattern/replacement} replace first match
${VARIABLE//pattern/replacement} replace all matches
$((expression)) replace the expression with its value
$[expression] # sustituye expresión por su valor
EXECUTION
./command execute from current directory
$ROUTE/command run from anywhere
command # executes if it's in the $PATH
.script execute exporting variables
$(command param1 ...paramN) executes literally
command param1 ...paramN run by substituting variables
command & run in the background
c1 |c2 redirect output c1 to input c2
c1 ;c2 execute c1 and then c2
c1 && c2 execute c2 if c1 finishes without errors
c1 ||c2 execute c2 if c1 ends with errors
COMMAND LINE ARGUMENTS
while getopts "hs:" option ; do #getops + "available options"
case "$option" in as long as there are arguments
h) DO_HELP=1 ;; # we select
s) argument=$OPTARG; DO_SEARCH=1 ;; # -h without options
Invalid # -s with options in $OPTARG
case # * error
done

Autor: Adolfo Sanz De Diego [Link] License:CC-BY-SA


CONTROL STRUCTURES
if expression1; then #conditional
block1 if expression1 then
elif expression2; then # block1
block2 if and expression2 then
else # block2
block3 if none then
fi # block2
caseVARIABLE in #selective
pattern11|...|pattern1N) if VARIABLE matches patterns1
block1 # so block1
pattern21|...|pattern2N) if VARIABLE matches patterns2
block2 # so block2
*) if none
defaultBlock # then blockDefault
case
for VARIABLE in LISTA; do #iterative with list
block execute block replacing
done # VARIABLE for each element of LIST
for ((expr1; expr2; expr3;)); do #iterative with counter
bloque first, exp1 is evaluated
done then while exp2 is true
the block and expr3 are executed
while expression; do while loop
block block executes
done while the expression is true
until expression; do do while loop
expression block is executed
done until the expression is true
[function]expression () { function
[ return [value] ] it is invoked with
} # functionName [param1 ... paramN]
INTERACTIVITY
read [-string] [variable1 ...] #input
read keyboard and assign to variables
a message can be shown beforehand
if no variable, REPLY = everything
echo string #output
-n does not create a new line send the value of the string
- and interpret characters with to the standard output
printf #formatted output (same as C)
PROCESS CONTROL
command & run in the background
bgnúmeroProceso continues execution in the background
fgnúmeroProceso continue execution in the foreground
jobs show running processes
kill signal PID1|númeroProceso1 kill the indicated process(es)
exit code exit with return code
(0=normal, >0=error)
trap [command] [code1 ...] execute command when signal(s)
wait [PID1|processNumber1] wait until the end of child process(es)
nice-n priority command execute command with priority [-20/19]
renice-n priority command modify command priority [-20/19]
-20 maximum priority and 19 minimum

Autor: Adolfo Sanz De Diego [Link] License:CC-BY-SA

You might also like