LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 02-28-2004, 09:09 AM   #1
naka0naka
LQ Newbie
 
Registered: Feb 2004
Posts: 4

Rep: Reputation: 0
bash shell script


I was playing with the following script and it did not produce the expected result.

#! /bin/bash
cd /
echo HELLO


I saw HELLO printed on the screen but my current working directory was not at the root directory. Any idea what went wrong?
 
Old 02-28-2004, 09:22 AM   #2
ranger_nemo
Senior Member
 
Registered: Feb 2003
Location: N'rn WI -- USA
Distribution: Kubuntu 8.04, ClarkConnect 4
Posts: 1,142

Rep: Reputation: 47
The script doesn't run exactly as if you typed in the commands yourself... When it's done, it's returning you to where you were. You can check this by sticking a new line in the script before and after the cd...

echo $PWD

This will print out the current directory the script is in. You will see that it starts in your current directory, then moves to root.
 
Old 02-28-2004, 09:43 AM   #3
naka0naka
LQ Newbie
 
Registered: Feb 2004
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks. I got it.

Is there anyway i can make the change permanent?
 
Old 03-02-2004, 12:19 PM   #4
ranger_nemo
Senior Member
 
Registered: Feb 2003
Location: N'rn WI -- USA
Distribution: Kubuntu 8.04, ClarkConnect 4
Posts: 1,142

Rep: Reputation: 47
Maybe, but I don't know it.
 
Old 03-02-2004, 12:26 PM   #5
markjuggles
Member
 
Registered: Nov 2003
Location: Chicago western suburbs
Distribution: Linux Mint
Posts: 75

Rep: Reputation: 15
Here are two ideas:

1. "Dot Space" your command: $ . my_script

This will run the script in the current shell. By default your scripts run in a new shell which gets destroyed when it's done. That is why your environment is unchanged.

2. Write an alias for your utility rather than a script. I haven't done this with bash but it works for ksh. Look for 'alias' in the bash man page.

Mark
 
Old 03-03-2004, 12:22 PM   #6
naka0naka
LQ Newbie
 
Registered: Feb 2004
Posts: 4

Original Poster
Rep: Reputation: 0
thanks
 
Old 05-28-2004, 01:10 PM   #7
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 34
The unix source command also does what the dot command does.

Code:
source ./myscript
# is the same as
. ./myscript
 
Old 05-28-2004, 03:06 PM   #8
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,492

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
For more than one line scripts, shell functions supersedes the alias command.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
help with bash shell script !! taiwf Linux - Newbie 5 06-11-2006 06:07 PM
Bash shell script moodupani Linux - General 2 09-01-2005 06:14 AM
Bash Shell script bd1308 Linux - Hardware 9 08-22-2005 05:38 PM
bash shell script globeTrotter Linux - Newbie 5 06-03-2004 05:07 AM
bash/shell script mikis Programming 2 10-14-2003 11:39 AM


All times are GMT -5. The time now is 12:40 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration