Bash:Variables: Difference between revisions

From wiki
Jump to navigation Jump to search
Created page with "Bash is a *nix shell To check within a startup script (like .bashrc) whether or not Bash is running interactively. Test if $- has an 'i' in it or if $PS1 (the prompt) exists...."
 
 
(15 intermediate revisions by the same user not shown)
Line 1: Line 1:
Bash is a *nix shell
[[Category:Linux/Unix]]


To check within a startup script (like .bashrc) whether or not Bash is running interactively. Test if $- has an 'i' in it or if $PS1 (the prompt) exists.
=Arithmetics=


Bash and ksh only work with integers.
;+, -, *, /, %
:add, substract, multiply, divide, modulo.
;<nowiki>**</nowiki>
:For bash, Exponentiation
;10#00012
:For bash, force the figure (00012) to be interpreted as decimal. Figures starting with 0 are considered octal by default.
;expr <expr>
:Voer expr uit, bij boolean wordt 1 (onwaar) of 0 (waar) gegeven
;((VAR=x+y))
;let VAR=x+y
;VAR=$((x+y))
;VAR=$(expr x + y)
:Calculate and assign to VAR
;let VAR+=2
;VAR=$((VAR += 2))
:Add two to VAR
=Special Parameters=
;$0
:Name current procedure
;$1
:First passed argument ($2 is the second, $3... )
;$*
:All arguments given as one string
;$@
:All arguments given as separate strings per parameter
;$#
:The number of passed arguments
;$?
:The returnstate for the last executed command. 0 = OK, all others not.
;$$
:ProcessID of current process, handy to use for temporary files
;$!
:ProcessID of last spawned process.
=Nested variables=
;var2=${!var1}
:var2 will get the value of the evaluation of var1 (value of var0)
<syntaxhighlight lang=bash>
var0=1
var1=var0
var2=${!var1}
echo $var2
1
</syntaxhighlight>
=Loop over a range=
<syntaxhighlight lang=bash>
<syntaxhighlight lang=bash>
case "$-" in
for i in {00..59}
*i*) echo This shell is interactive ;;
do
*) echo This shell is not interactive ;;
  echo ${i}
esac
done
 
for i in $(seq ${START} ${END})
do
  echo $i
done
</syntaxhighlight>
 
=Operations on variables=
* # searches from the beginning
* % searches from the end
 
;${var#??}
:Remove the first 2 chars
;${var%??}
:Remove the last 2 chars
;${var%e*}
:Remove everything from and including the last 'e'
;${var#*e}
:Remove everything up to the first 'e'
;${var#* * }
:Remove the first 2 words from a line (carefully check the spaces)
;${var% * *}
:Remove the last 2 words from a line (carefully check the spaces)
;${#var}
:Length of the string


if [ -z "$PS1" ]; then
;Check if a string is in the variable
        echo This shell is not interactive
The trick is that if the variable does not have the searchstring the variable will be unchanged.
else
<syntaxhighlight lang=bash>
        echo This shell is interactive
if [ "${var#*searchstring}" != "${var}" ]
then
  echo "searchstring is in $var"
fi
</syntaxhighlight>
</syntaxhighlight>

Latest revision as of 18:49, 11 April 2022


Arithmetics

Bash and ksh only work with integers.

+, -, *, /, %
add, substract, multiply, divide, modulo.
**
For bash, Exponentiation
10#00012
For bash, force the figure (00012) to be interpreted as decimal. Figures starting with 0 are considered octal by default.
expr <expr>
Voer expr uit, bij boolean wordt 1 (onwaar) of 0 (waar) gegeven
((VAR=x+y))
let VAR=x+y
VAR=$((x+y))
VAR=$(expr x + y)
Calculate and assign to VAR
let VAR+=2
VAR=$((VAR += 2))
Add two to VAR

Special Parameters

$0
Name current procedure
$1
First passed argument ($2 is the second, $3... )
$*
All arguments given as one string
$@
All arguments given as separate strings per parameter
$#
The number of passed arguments
$?
The returnstate for the last executed command. 0 = OK, all others not.
$$
ProcessID of current process, handy to use for temporary files
$!
ProcessID of last spawned process.

Nested variables

var2=${!var1}
var2 will get the value of the evaluation of var1 (value of var0)
var0=1
var1=var0
var2=${!var1}
echo $var2
1

Loop over a range

for i in {00..59}
 do
  echo ${i}
 done

for i in $(seq ${START} ${END})
 do
  echo $i
 done

Operations on variables

  • # searches from the beginning
  • % searches from the end
${var#??}
Remove the first 2 chars
${var%??}
Remove the last 2 chars
${var%e*}
Remove everything from and including the last 'e'
${var#*e}
Remove everything up to the first 'e'
${var#* * }
Remove the first 2 words from a line (carefully check the spaces)
${var% * *}
Remove the last 2 words from a line (carefully check the spaces)
${#var}
Length of the string
Check if a string is in the variable

The trick is that if the variable does not have the searchstring the variable will be unchanged.

if [ "${var#*searchstring}" != "${var}" ]
 then
  echo "searchstring is in $var"
fi