DEV Community

Ashish Prajapati
Ashish Prajapati

Posted on

Intro to bash

Bash is a shell, or commend language interpreter, for the GNU operating system

Example. $USER and $PWD are variables

  • echo β€œprint” # to print something
val=1
val=$((val+1)) # it increases the value by 1
Enter fullscreen mode Exit fullscreen mode
  • We can use let val+=1 to increase its value
fix=2
for ((i = 1; i <= 99; i++)) 
do
    if [ $((i%fix)) -eq 1 ] 
    then
        echo "Number: $i"
    fi
done

output: 1, 3, 5, 7, 9, 11, ....., 99
Enter fullscreen mode Exit fullscreen mode
  • read is used to take input
  • Comparing numbers:
  read X
  read Y

  if [ $X -lt $Y ] ; 
  then
      echo "X is less than Y"
  elif [ $X -gt $Y ] ;
  then
      echo "X is greater than Y"
  else
      echo "X is equal to Y"
      fi
Enter fullscreen mode Exit fullscreen mode
  • Comparing multiple conditions:
read c
if [[ c=="Y" || c=="y" ]]
then
    echo "YES"
else 
    echo "NO"
    fi
Enter fullscreen mode Exit fullscreen mode

Top comments (0)