UnixDroid

Intermediate · 6 missions

Bash scripting — Linux terminal practice

Write and run your own shell scripts with nano: variables, loops and if-tests.

student@unixdroid: ~

Missions in this track

nano me apni script likho: variables, loops aur if conditions.

  1. 1

    Variables

    Store your name in a variable and print it: name=Rahul then echo "Hi $name".

    Show the command
    name=Rahul; echo "Hi $name"

    No spaces around =. Use $name to read the value.

  2. 2

    Write a script

    Open ~/hello.sh in nano and write echo "Hello $USER". Save with Ctrl+S, exit with Ctrl+X.

    Show the command
    nano ~/hello.sh

    Scripts are just text files full of commands.

  3. 3

    Make it executable

    Give ~/hello.sh execute permission.

    Show the command
    chmod +x ~/hello.sh

    Or run it without the x bit: bash hello.sh.

  4. 4

    Run your script

    Run ~/hello.sh.

    Show the command
    ~/hello.sh

    You just wrote your first program. 🎉

  5. 5

    Loops

    Print the numbers 1 to 5 with a for loop.

    Show the command
    for i in $(seq 1 5); do echo $i; done

    Loops automate boring, repeated work.

  6. 6

    If tests

    Print exists only if ~/hello.sh is a file: [ -f ~/hello.sh ] && echo exists.

    Show the command
    [ -f ~/hello.sh ] && echo exists

    -f file, -d folder, -e anything. Scripts use these tests to avoid errors.