LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-18-2003, 03:24 AM   #1
l0f33t
Member
 
Registered: May 2003
Location: Washington State
Distribution: Gentoo
Posts: 145

Rep: Reputation: 15
Cool BASH Confusion......Arrrrg


Hello all,

Running Linux Kernel: 2.4.19-16mdk
Bash version: 2.05b.0

I've been doing some online tutorials on BASH shell scripting.

I created a script called "foobar" in my home directory. I gave the script (chmod 755 foobar) permissions. Couldn't figure out how to run the script by just using the name.. foobar.

The script runs fine using "bash foobar" or "./foobar" or "sh foobar" or specifying the full complete path. /home/jbanks/foobar and of course putting the file in the /bin directory allows me to run the script by name "foobar"

Buttttt...I wanted to have the ability to exe the script from within my home directory by script name.

On the tutorial they said to just create a /bin directory inside my home directory and move the script into that directory. Done.

Logout then log back in. This is where I'm a little confused. It works....now but when I look at echo $PATH I now have 2 same path statements.. I didn't have to do anything to the .bash_profile either.

This was my path before creating the /bin directory:
[jbanks@localhost jbanks]$ echo $PATH
/usr/X11R6/bin:/usr/local/bin:/bin:/usr/bin

AFTER

[jbanks@localhost jbanks]$ echo $PATH
/bin:/usr/bin:/usr/bin/X11:/usr/X11R6/bin:/usr/local/bin:/usr/bin:/usr/games:/home/jbanks/bin:/home/jbanks/bin

Why is /home/jbanks/bin listed twice?

When I login as root a notice the same type of similarity:
[root@localhost jbanks]# echo $PATH
/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin:/usr/local/bin:/usr/local/sbin

Thanks for your help.
Heres a copy of my .bash_profile and .bashrc. Let me know if you need any futher info?

.BASH_PROFILE:
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH
unset USERNAME

.BASHRC:
# .bashrc

# User specific aliases and functions

# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi


Thanks,

Last edited by l0f33t; 08-18-2003 at 03:57 AM.
 
Old 08-18-2003, 03:50 AM   #2
hfawzy
Member
 
Registered: Aug 2002
Location: Egypt
Distribution: Debian Sarge, Slackware 10.0
Posts: 163

Rep: Reputation: 30
Hi,
Quote:

.BASH_PROFILE:
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH
unset USERNAME

.BASHRC
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH
unset USERNAME
Did you posted your .bash_profile twice?
If no, delete the second part of this file and it will look like that :
Code:
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH
unset USERNAME
Hope that helps...
 
Old 08-18-2003, 03:59 AM   #3
l0f33t
Member
 
Registered: May 2003
Location: Washington State
Distribution: Gentoo
Posts: 145

Original Poster
Rep: Reputation: 15
Woops...

I posted that incorrectly here. I've edit'ed my initial post.

Any clues?

Thanks,
 
Old 08-21-2003, 02:00 AM   #4
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Firstly, I would recommend you NOT make the change. The reason the current directory isn't in the path by default, is the risk someone could place a destructive script in a directory using the name of a command like ls. But all you need to do is add "./" in your path variable.

PATH=$PATH:$HOME/bin:./

I must confess to typing in PATH=$PATH:./ during a single session. But I would change any shell startup scripts do make the change permanent.

Last edited by jschiwal; 08-21-2003 at 02:06 AM.
 
Old 08-21-2003, 02:23 AM   #5
whansard
Senior Member
 
Registered: Dec 2002
Location: Mosquitoville
Distribution: RH 6.2, Gen2, Knoppix,arch, bodhi, studio, suse, mint
Posts: 3,304

Rep: Reputation: 65
source ~/foobar

foobar doesn't need to be 755 for that either.


sorry. i just reskimmed your post, and i don't think
thats what you wanted.

you could also
ln -s /path/foobar /usr/local/bin/

Last edited by whansard; 08-21-2003 at 02:30 AM.
 
Old 08-21-2003, 03:19 AM   #6
l0f33t
Member
 
Registered: May 2003
Location: Washington State
Distribution: Gentoo
Posts: 145

Original Poster
Rep: Reputation: 15
Thanks for the help everyone.

jschiwal,

Thanks for the Security tip.
You said:
But all you need to do is add "./" in your path variable PATH=$PATH:$HOME/bin:./

Is this done to the .bash_profile or the .bashrc file?
Do I just append :./ to the exsisting Path variable specified in the file/files above?

Why is ./ more secure than having /home/jbanks/bin?

whansard,

What is the difference between "sourcing" a file and "executing" file? They seem the same to me..

Thanks,
 
Old 08-21-2003, 03:59 AM   #7
whansard
Senior Member
 
Registered: Dec 2002
Location: Mosquitoville
Distribution: RH 6.2, Gen2, Knoppix,arch, bodhi, studio, suse, mint
Posts: 3,304

Rep: Reputation: 65
i don't really know.
source is a bash builtin. you can run bash source with
it without making the file executable.
 
Old 08-21-2003, 04:05 PM   #8
l0f33t
Member
 
Registered: May 2003
Location: Washington State
Distribution: Gentoo
Posts: 145

Original Poster
Rep: Reputation: 15
Thanks,

I guess I'll put mv all my scripts in /home/jbanks/bin to the root /bin file for security reasons and remove the /home/jbanks/bin directory so that path isn't excessible..

JBanks
 
Old 08-21-2003, 04:48 PM   #9
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
By your response, I think I could have explained things better.

You question was about why you had to precede a command with ./ to get it to run.

Adding ./ to your path variable is the way to include the current directory ($PWD ) in the path.

If for example, you are in a subdirectory, and write a quick shell script to use, having ./ in your path allows you to type in the script without preceding the command with ./ or having to copy the script to ~/bin/.

I didn't want to imply that this change would be secure.

If you do make the change, I would recommend that you enter it into the current terminal you are using only rather than change one of the startup scripts. If you are working in a subdirectory you just made, or the ls listing doesn't show any files added by a hacker, then it should be safe and the path variable change only effects the terminal you are current using. When you exit, your change will be lost.

One other thing you might want to consider. Suppose you add a disk drive, and you are the owner, or group member. If you know that this drive will only contain your data files, and never programs or scripts, consider including the nosuid,noguid and noexec option to the mount command.

Sometimes, a problem can occur due to a typo or other mistake and not to a malicious hacker or network worm. Taking precautions may also help limit damage made accidentally.
 
Old 08-21-2003, 05:04 PM   #10
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
When I reread the thread, I realized that the original question wasn't answered. I'm not a home right now, so I can't refer to the bash documentation. But I would recommend that you consider that there are different bash startup files.

One of them is used once when you login as a user. This is the one you want to use to make your $PATH variable adjustment.

Another one is used every time you start a new shell. I think that the $PATH adjustment is being run more then once, because it's in the wrong startup file, or is being executed more than once.

Look in MAN BASH for details on the startup scripts. And use the one that is used once when you log in.
 
Old 08-21-2003, 05:12 PM   #11
l0f33t
Member
 
Registered: May 2003
Location: Washington State
Distribution: Gentoo
Posts: 145

Original Poster
Rep: Reputation: 15
Thanks,
I'll take a look at "man bash" then.

Thanks jschiwal,
 
  


Reply



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
Cryptsetup : Arrrrg ! kemkem42 Linux - Software 0 09-28-2005 04:10 AM
shell confusion..what is diff between bash, ksh, csh, tcsh..?? servnov Linux - Newbie 7 11-18-2004 08:28 PM
why did bash 2.05b install delete /bin/bash & "/bin/sh -> bash"? johnpipe Linux - Software 2 06-06-2004 06:42 PM
WM Confusion phoeniXflame Slackware 3 02-16-2003 05:19 AM
Some confusion about RH 7.3 psyklops Linux - Distributions 2 05-08-2002 03:08 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:34 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration