mltefive / docs

https://mltefive.github.io/docs/
1 stars 0 forks source link

bash cheats https://devhints.io/bash #5

Open 7dir opened 5 years ago

7dir commented 5 years ago
for i in $(seq 1 $END); do echo $i; done
7dir commented 5 years ago

get_name() {
  echo "John"
}

echo "You are $(get_name)"

{A,B} | Same as A B
-- | --
{A,B}.js | Same as A.js B.js
{1..5} | Same as 1 2 3 4 5

echo "I'm in $(pwd)"
echo "I'm in `pwd`"

NAME="John"
echo "Hi $NAME"  #=> Hi John
echo 'Hi $NAME'  #=> Hi $NAME

if [[ -z "$string" ]]; then
  echo "String is empty"
elif [[ -n "$string" ]]; then
  echo "String is not empty"
fi

Basics
name="John"
echo ${name}
echo ${name/J/j}    #=> "john" (substitution)
echo ${name:0:2}    #=> "Jo" (slicing)
echo ${name::2}     #=> "Jo" (slicing)
echo ${name::-1}    #=> "Joh" (slicing)
echo ${name:(-1)}   #=> "n" (slicing from right)
echo ${name:(-2):1} #=> "h" (slicing from right)
echo ${food:-Cake}  #=> $food or "Cake"
7dir commented 5 years ago

How do I check whether a variable has an even (not odd) numeric value in bash?


foo=6

if [ $((foo%2)) -eq 0 ];
then
    echo "even";
else
    echo "odd";
fi

a=4
[ $((a%2)) -eq 0 ] && echo "even"