LINUX CLASSES - PROGRAMMING
Linux Shell Scripts
How Do I Run a Linux Shell Script?
So how do you run this little wonder of technology? In DOS, all you have to do is name a
file with a .bat extension and it'll be recognized as an executable file--but not so with
Linux. Since Linux attaches no meaning to file extensions, you have to mark the file as
executable by using the chmod command, like this:
$ chmod +x deltemp
The x marks the file as executable; if you list the permissions for the deltemp file
afterward, you will see the x in position four, confirming this:
$ ls -l deltemp
-rwx------ 1 hermie other 55 Feb 19 14:02 deltemp
If you want other users to be able to run this script, give them both read and execute
permission, like so:
$ chmod ugo+rx deltemp
$ ls -l deltemp
-rwxr-xr-x 1 hermie other 55 Feb 19 14:04 deltemp
Now the permissions show that any user can view or execute the deltemp script, but only
you can modify it. To run the script, just enter its name at the command prompt, prefixed
with ./ as shown here:
Note: If the current directory is in the PATH environment variable, you can omit the ./
before the name.
But there's one important thing you should know about running shell scripts this way.
When you enter the shell script name and tell Bash to run it, a subshell is created for the
execution of the script. This subshell inherits all the shell environment variables,
including the current directory, but you lose any changes made to that environment when
the script terminates.
What's the practical meaning of this? Well, you might expect that the current directory
would be /tmp after you've run the deltemp script, but it's not. You'll still be in
hermie's home directory. And if we had set an environment variable inside the script, its
value would be good only during the execution of the script. Here's an example to
demonstrate this point. Create an executable setvar script with these lines:
PS1='My Prompt: '
Now watch how the values of the current directory and the PS1 variable change:
$ PS1='Bash Me! '
$ echo $PS1
Bash Me! PS1 before setvar.
My Prompt: PS1 during setvar.
$ echo $PS1
Bash Me! PS1 after setvar.
It looks like this script is absolutely useless for the intended purpose of setting the
environment variable. But a little trick will make it work the way you want. Run the
script by prefixing the name with a dot and a space, like so:
This tells Bash to run the setvar script in the current shell environment, instead of
creating a temporary subshell. Verify for yourself that the command prompt does in fact
change to My Prompt: after running this script.
Previous Lesson: Linux Shell Scripts
Next Lesson: Shell Script Variables
[ RETURN TO INDEX ]
Comments - most recent first
(Please feel free to answer questions posted by others!)
(04 Mar 2013, 10:20
How can i create and run a shell program in linux terminal................
please any one send the steps...
(06 Nov 2012, 14:24
I am executing bash script from C program and exporting some variables but
value of this exported variable are not actually exported as i checked with
echo command after executing the C program.So C program is also unable to
read this values using getenv command.
(19 Mar 2012, 05:30
please send the shell scripts examples
(20 Jan 2012, 00:55
I'm using some software for my work.
When I invoke that software, it is invoking from my home location.
While working, software saves lot of intermediate files at my home
location, creating lot of garbage at home.
I wanted to know is there any way to create these intermediate files at
some other location other than home.
Thanks in advance.
(04 Jan 2012, 04:38
Hi bob thanks a lot for giving me the best understanding about shell
(03 Jan 2012, 06:08
i want to run a linux script on windows machine which it should login to
linux machine and fetch the details and output should redirected to windows
machine....is it possible?
(29 Aug 2011, 04:39
Fantastic Linux website!!! is there anyway to have in a pdf format?
I was wondering if there is a chance for bash lessons
Many thanks for the site
(15 Jul 2011, 01:08
Thanks so much Bob!
(04 Jul 2011, 23:24
my i get sample script for run fortran 90 at linux...
(09 Jun 2011, 00:11
I Want learn Linux please help me
(29 Apr 2011, 08:50
Same method you can execute the perl scrip .
(10 Feb 2011, 18:06
What is the command to execute a perl script on a Linux server
(14 Jan 2011, 09:12
Bob .. your suggested command helped me too .. thanks a lottt ....
(07 Dec 2010, 16:41
i have a file (myfile) containing functions.
execting the file with ./myfile or using bash myfile
failed to make the functions available to the shell
but (. myfile) worked. why???
J. van der Meer
(23 Nov 2010, 14:21
Your explanation of the different commands, shell scripts and others are a
Even I can learn to do the trics now.
Thanks once again
(04 Oct 2010, 22:32
Thanks for the well thought out and planned articles. I have learned more
about the inner workings of Linux in a few days by reading your work than I
have in the past couple of years reading man pages.
(14 Sep 2010, 10:18
Thanks for this in depth tutorial. I am having a similar problem while
executing a script with in a script. My original script begins with
I have two scripts inside it .
1)one is for setting the environment(actually links the Linux box to a
repository) for code extraction. It has to be run through
.(Dot) scriptname (running in the current shell).
2)Another script is (when both are linked) to work on the extracted stuff.
When i execute these from my own script
IT is logging in as rootuser bit not running the other two scripts.
Your help is highly appreciated.
(21 Aug 2010, 17:46
@sandeep - That's not enough information to diagnose your problem. Saying
"not behaving like root" is like "sometimes my car doesn't work." What
specifically is the problem?
(21 Aug 2010, 09:20
i am logging in as root ....
but it is not executing all commands that a root can do.it is displaying as
"root#" but not behaving like root..........
(20 Aug 2010, 02:44
thanks for ur tutorial.........
(02 Aug 2010, 11:13
Also, the sudo command might be helpful.
(02 Aug 2010, 11:10
@Rahul - The su command cannot accept a password, but you could use ssh to
connect to localhost, and pass the command. The ssh command can store
passwords with ssh-keygen, and eliminate the need to key it in.
(01 Aug 2010, 12:58
can i gave the password in my script automatically for ex.
i want to switch from user rahul to oracle using
su - oracle
then how it get the password
(04 Jul 2010, 13:30
I want to run executables by just typing the name of the executable file
instead of typing ./ first (./name). I want to write a shell for this and
place it in the /usr/bin directory. Can you give me the code?
(02 Jun 2010, 06:14
@hari - That's like cutting off the branch you're standing on... you'll
both fall to the ground. You might consider saving the task ID of the
first task in a file, then launching the second task as a background task
(not nested). Then the second task can read the task ID of the first from
the file, and issue a 'kill' command. Still, it seems there is probably a
cleaner way to do what you want.
(02 Jun 2010, 03:43
Hi....i've one script nested inside another script...i need to stop the
execution of the outer script once an if condition in the inside script is
satisfied..how can i stop running this outer script??any command for it???
(01 Jun 2010, 10:35
@Dimitris - That can certainly be done. Most likely your script is not
running in the correct directory. Try using absolute addressing for the
directory in your script.
(01 Jun 2010, 03:06
Hi , i am trying to write a script that executes some commands. i want to
have my script in a folder an through the script i want to be able to use
the cd command to move into the subfolders. can this be done ? because i
use the cd and i receive an error the folder does not exist.
(14 May 2010, 05:56
I think if you use su without the -c flag, it opens a new shell and your
script is suspended until you return. I don't know what you mean about
automating the password.
(13 May 2010, 07:35
Sorry about my questions Bob, can i automate password in a linux script?
(13 May 2010, 07:31
Thanks so much Bob, it worked exactly as you said. It thus, executes the
commands as the other user but returns to the original user afterwards. Is
there a way I can remain as the user i switch to?
(16 Mar 2010, 14:28
Try this: su - username -c "command1;command2;..."
(16 Mar 2010, 12:29
i wrote a script in Linux. Included in the script was "su - user".
I noticed the script runs until it switches user and then stops. Refusing
to execute the commands after the su command. What might be the the
I welcome your comments. However... I am puzzled by many people
who say "Please send me the Linux tutorial." This website *is* your Linux Tutorial! Read everything here, learn
all you can, ask questions if you like. But don't ask me to send what you already have. :-)
NO SPAM! If you post garbage, it will be deleted, and you will be banned.
by Bob Rankin
All rights reserved - Redistribution is allowed only with permission.