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
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.
Comments - most recent first
(Please feel free to answer questions posted by others!)
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.
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
execting the file with ./myfile or using bash myfile
failed to make the functions available to the shell
but (. myfile) worked. why???
Your explanation of the different commands, shell scripts and others are a releave.
Even I can learn to do the trics now.
Thanks once again
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.
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 #!/bin/ksh.
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.
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..........
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
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?
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 problem?
NO SPAM! If you post garbage, it will be deleted, and you will be banned.
Copyright © by -
All rights reserved - Redistribution is allowed only with permission.