• +4917626969472
  • info@ur-techpartner.de

Learn Basic Commands for LINUX systems – 03/04

Basic Linux for ROS Drone

Basic Commands for LINUX Systems – 03

Bash scripts

So far we have run commands ( e.g. cd, cp, ls etc) on command line. However Linux gives us facility to put these commands in a script (or file). These commands will do exactly the same thing as they do on command line. These scripts are known as bash scripts.

When we have multiple commands that we want to run at the same time (and multiple times), we put them into bash script. The extension of these bash files is “.sh”

let’s create a bash file in catkin_ws/my_new_folder

Go to the folder and create file name “bash_file.sh”

cd catkin_ws/my_new_folder
touch bash_file.sh

Now open and edit the file using “gedit” command

gedit bash_file.sh

Enter following lines into the file, then save and close it.


echo “my first bash file”

as show in following image

Then run the bash file. Write following command on terminal (add “./” before file name to run bash file)


let’s see what happens

Oh! there is an error, which states “bash: ./bash_file.sh: Permission denied”.

Since permission is denied, it means we will have to do something with permissions assignment.

As we are already familiar with “ls” command. We can also check permissions of files/folders using this command by adding “-la” argument.

let’s use it

ls -la

we can see that first column has entries something like this ”-rw-rw-r–”

Actually these show the permissions on files/folders. Here “w” means write permission, and “r’ means read permissions. While “x” means execution permission. But in our case only


is displayed for “bash_file.sh” file. Which means the files doesn’t have execution permission.

We assign execution permission using following command and run the file again

chmod +x bash_file.sh

Yes, it worked, and output is as follows

my first bash file

We can also check what are new permission assigned to “bash_file.sh” file.

ls -la

We can see the corresponding column entry is changed from “rw-rw-r–” to “rwxrwxr-x”. it is because of running the command “chmod +x”

**First we will complete the bash script topic then we will see how we can assign or remove permissions to files/folders.**

Now we see what is inside the “bash_file.sh” file.

It is


echo “my first bash file”

Here first line is “#!/bin/bash”. All bash scripts should start from this line. It tell Linux system that this file is a bash script. The next line “echo ‘my first bash file’” is just a print line as we have seen in output.

Passing parameters to bash script

Write following code in file “bash_file.sh”



if [ $ARG1 == 'Faisal' ]; then

        echo "My name is Faisal, and I am in Europe"

elif [ $ARG1 == 'Kashif' ]; then

        echo "My name is Kashif, and I am in Asia"


Save the file and now run it along with parameters as follows

./bash_file.sh Faisal
./bash_file.sh Kashif

We can pass arguments (parameters) on command line as shown before. These arguments can be accessed in bash file using $1, $2, $3, and so on.

Are you are wondering what should be written in bash script and what should be run on command line. No worries! we can safely say that, everything that we run on command line can be put into bash script and everything that can be put in bash script can be run on command line, and in both cases same thing will happen.


As we have already seen the command “chmod” is used to assign/remove the permission to file/folders.

The syntax of the command is as follows

chmod <group to assign the permission (its optional)> <permission to assign/remove> <file/folder name>

We already know that there are three kinds of permissions

x: execute

w: write

r: read

The plus sign “+” is used to assign the permission and the minus “-” sign is used to remove the permission.

Similarly we have three categories as permission groups, which are represented in these nine characters: rwxrwxrwx. When, we check the permission, r or w or x means permission is already assigned, if permission is removed (or not assigned), then “-” will be shown instead or r/w/x characters.

Owner (u): These permissions apply only to the owner of the file/folder. They are represented in the first 3 characters.

Group (g): These permissions apply only to the group that has been assigned to the file/folder. They are represented in the middle 3 characters.

Other users (o): These permissions apply to all other users on the system. They are represented in the last 3 characters.

All users (a): If we use “a” or leave blank, then permissions will be applied to all users (as mentioned above).

We can run different commands to practice the assignment/removal of permissions.

For example

chmod u-w bash_file.sh

will remove the write permission from the file for the “owner”.


chmod g-x bash_file.sh

will remove execution permission from group user.

Likewise the line

chmod +w my_new_file.py

will assign writing permission to all users on given file.

No comments