====== Examples of Variables and Arrays in Bash ======
var="http://wiki.plecko.hr/"
echo $var
http://wiki.plecko.hr/
echo ${#var}
31
echo ${!var}
echo ${!U*}
UID USER USERNAME
echo ${var:7}
wiki.plecko.hr/
echo ${var:11:6}
plecko
echo ${var#http://}
wiki.plecko.hr//
echo ${var%.hr/}
http://wiki.plecko
echo ${var/plecko/eurekamoment}
http://wiki.eurekamoment.hr/
echo ${var//o/O}
http://wiki.pleckO.hr/
echo ${var/#http/Site}
Site://wiki.plecko.hr/
var2="test"
echo ${var2/%st/STING}
teSTING
echo "$var"
http://wiki.plecko.hr/
echo '$var'
$var
echo $'$var\n'
$var
echo `ls`
Desktop Documents Downloads Images Templates Music Public Videos
(ls)
Desktop Documents Downloads Images Templates Music Public Videos
echo $(ls)
Desktop Documents Downloads Images Templates Music Public Videos
((11>9))
echo $((11-9))
2
[[ $var ]] && echo 'Its bigger'
Its bigger
[[ $var ]] && echo 'Is there this variable'
Is there this variable
Variable Positional Parameters
$0 Parameter Number 0 (Name of Command or Function)
$1 Parameter Number 1 (from command line or function)
... Parameter number N …
$9 Parameter Number 9 (from command line or function)
${10} Parameter Number 10 (from command line or function)
... Parameter number NN …
$# Total number of command line or function parameters
$* All parameters as a single string
$@ All parameters, such as multiple protected strings
Variable Miscellania
$$ PID number of current process (from script itself)
$! PID number of last background job
$_ Last argument of last command executed
$? Return code from last command executed
Escape Reminder Expands to …
\a Alert Alert (beep)
\d Date Date in "Weekday Month Day" format (Sat Jan 15)
\e Escape Esc Character
\h Hostname Machine Name Without Domain (dhcp11)
\H Hostname Full Machine Name (dhcp11.company)
\j Jobs Number of Active Jobs
\l Tty Current Terminal Name (ttyp1)
\n Newline Newline
\r Return Return by car
\s Shell Name of the shell (basename $ 0)
\t Time Time in 24-hour format HH: MM: SS
\T Time 12-hour format HH: MM: SS
\@ At Time in 12-hour format HH: MM am/pm
\A At Time in 24-hour format HH:MM
\u User Current user login
\v Version Bash Version (2.00)
\V Version Bash Version Subversion (2.00.0)
\w Working Dir Current directory, full path ($PWD)
\W Working Dir Current directory, only the last one (basename $PWD)
\! History ico Current command number in history
\# Number Current command number
\$ ID > Show "#" if root, "$" if normal user
\nnn Octal Character whose octal is nnn
\\ Backslash Backslash \ literal
\[ Escapes Starts a sequence of escapes (color coded type)
\] Escapes Ends an escape sequence
Format Description
%a Abbreviated Weekday Name (Sun..Sab)
%A Name of the day of the week (Sunday..Saturday)
%b Abbreviated Month Name (Jan. Dec)
%B Name of the month (January.December)
%c Complete date (Sat Nov 04 12:02:33 EST 1989)
%y Year (two digits)
%Y Year (four digits)
%m Month (01..12)
%d Day (01..31)
%j Day of the year (001..366)
%H Hours (00..23)
%M Minutes (00..59)
%S Seconds (00..60)
%s Seconds since January 1, 1970
%% A % literal
%t One TAB
%n A line break
Format Description
%d Decimal number
%o Octal Number
%x Hexadecimal Number (a-f)
%X Hexadecimal Number (A-F)
%f Floating-point number
%e Number in scientific notation (e + 1)
%E Number in scientific notation (E + 1)
%s String
set
#or, and open txt to see later
set > VariablesLocations.txt
env
#or
printenv
LINUX=free
echo $LINUX
free
set | grep LINUX
LINUX=free
export LINUX
env | grep LINUX
LINUX=free
unset LINUX
echo $LINUX
alias list='ls -la color=auto'
unalias list
history
#run command by his number in history
!468
#execute last command typed
!!
#they stay in bash_history
cat ~/.bash_history
#clear history
history -c
$ - Shell of an normal user;
# - Superuser shell root (administrator)
#Check of Available Shells
cat /etc/shells
#Variable that shows the SHELL you use
echo $SHELL
DISTROS=("Debian" "Trisquel" "Ubuntu" "RedHat")
#If you print the DISTROS Array as a variable, it prints variable 0, the array displays the variables contained in it starting at 0 (zero), so it would be the same as printing at ${DISTROS[0]}
echo $DISTROS
echo ${DISTROS[1]}
Trisquel
DISTROS[0]="Debian"
DISTROS[1]="Trisquel"
DISTROS[2]="Ubuntu"
DISTROS[3]="RedHat"
DISTROS[2]="Linux Mint"
echo ${DISTROS[2]}
Linux Mint
echo ${DISTROS[@]:2}
Ubuntu RedHat
echo ${DISTROS[@]:1:2}
Trisquel Ubuntu
echo ${#DISTROS[@]}
4