bash variable code example
Example 1: echo variable bash
echo "${var}"
Example 2: $@ bash
file:test.sh
echo '$#' $#
echo '$@' $@
echo '$?' $?
*If you run the above script as*
> ./test.sh 1 2 3
You get output:
$# 3
$@ 1 2 3
$? 0
*You passed 3 parameters to your script.*
$# = number of arguments. Answer is 3
$@ = what parameters were passed. Answer is 1 2 3
$? = was last command successful. Answer is 0 which means 'yes'
Example 3: shell script variable
TEXT="Hello World!"
echo $TEXT
Example 4: definition varibale in bash
fullname='Restu Wahyu Saputra'
echo $fullname
Example 5: bash create variable
name="Lance Armah"
Example 6: assign a variable in bash
#!/bin/bash
echo
a=879
echo "The value of \"a\" is $a."
let a=16+5
echo "The value of \"a\" is now $a."
echo
echo -n "Values of \"a\" in the loop are: "
for a in 7 8 9 11
do
echo -n "$a "
done
echo
echo
echo -n "Enter \"a\" "
read a
echo "The value of \"a\" is now $a."
echo
exit 0