set variable bash code example
Example 1: bash create variable
#create a variable to store name
name="Lance Armah"
Example 2: assign a variable in bash
#!/bin/bash
# Naked variables
echo
# When is a variable "naked", i.e., lacking the '$' in front?
# When it is being assigned, rather than referenced.
# Assignment
a=879
echo "The value of \"a\" is $a."
# Assignment using 'let'
let a=16+5
echo "The value of \"a\" is now $a."
echo
# In a 'for' loop (really, a type of disguised assignment):
echo -n "Values of \"a\" in the loop are: "
for a in 7 8 9 11
do
echo -n "$a "
done
echo
echo
# In a 'read' statement (also a type of assignment):
echo -n "Enter \"a\" "
read a
echo "The value of \"a\" is now $a."
echo
exit 0
Example 3: print variable in bash
#!/bin/bash
echo
echo "When single quote is used with string:"
invitation='Welcome to javatpoint'
echo $invitation
echo
echo "When double quote is used with string:"
invitation="Welcome to javatpoint"
echo $invitation
echo
echo "When variable is used with double quote:"
Remark="Hello User!, $invitation"
echo $Remark
echo
echo "When variable is used with single quote:"
Remark='Hello User!, $invitation'
echo $Remark
echo