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 06-22-2012, 07:17 PM   #1
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Rep: Reputation: 16
run program from any directory


I have a small script that i use to check the name of processes.
I can oly use it in my home directory.

So when i am in another directoy, I have to type in
Code:
/home/casper/my_program
or cd to my home directory and then type ./my_program

Is there a way that I could configure my path so I could run the program from any directory?
 
Old 06-22-2012, 07:30 PM   #2
jv2112
Member
 
Registered: Jan 2009
Location: New England
Distribution: Arch Linux
Posts: 719

Rep: Reputation: 106Reputation: 106
Lightbulb

Edit your

~user/.bashrc


if you want the change to only effect that user


/etc/bash.bashrc

if you want to make it system wide


Code:
sudo vim ( or whatever text editor you perfer ) /etc/bash.bashrc

look for a line like this

PATH=/usr/local/sbin:/usr/lib:/usr/lib64:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/X11R6/bin:/home/user/Scripts:/opt/java/jre

add the directory where your script is. Seperate the previous entry with :

The same method for ~user/.bashrc
 
Old 06-22-2012, 10:13 PM   #3
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Original Poster
Rep: Reputation: 16
So yeah - I don't see a ~user/.bashrc in my work directory
or a /etc/.bashrc

casper@casperbox /home/casper $ ls -ltr /.bashrc
ls: /.bashrc: No such file or directory

my bash seems to work just fine without it.
So I could just create a file /home/casper/.bashrc
add the example that you wrote, and it wont effect other users.

this is a work host - so I really can't screw around.
 
Old 06-23-2012, 07:18 AM   #4
knudfl
LQ 5k Club
 
Registered: Jan 2008
Location: Copenhagen DK
Distribution: PCLinuxOS2023 Fedora38 + 50+ other Linux OS, for test only.
Posts: 17,519

Rep: Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641
Quote:
casper@casperbox /home/casper $ ls -ltr /.bashrc
Wrong. There is no /.bashrc, but .bashrc

These commands will show your .bashrc :
$ ls -al .bash*
$ ls -ltr .bashrc
$ ls -ltr /home/casper/.bashrc
$ find . -name "*bashrc*"

.
 
Old 06-23-2012, 07:57 AM   #5
jv2112
Member
 
Registered: Jan 2009
Location: New England
Distribution: Arch Linux
Posts: 719

Rep: Reputation: 106Reputation: 106
Lightbulb



The path (location of bashrc) is relative to where you are.

If you are in /home/user then .bashrc (dot is default to hide filesa)

If you are not in your home directory then /home/usr/.bashrc


Modifications to your bashrc in your home directory will only impact you and nobody else so you should be safe. If you don't like the changes you can always modify.
 
Old 06-23-2012, 10:45 AM   #6
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
While you're in the process, you should probably move my_program out of your base home directory and into a subdir that will only contain other programs you've written. You don't want to just add your entire home directory to your PATH or things can get ugly. Make a new folder such as ~/bin, move my_program into there, and then add that directory to your PATH in ~/.bashrc
 
Old 06-23-2012, 10:49 AM   #7
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by casperdaghost View Post
So yeah - I don't see a ~user/.bashrc in my work directory
That's because it's not ~user/.bashrc. Maybe it was a typo in jv2112's post, not sure, but ~user/ isn't even a valid location. The file is located in either /home/user/.bashrc or ~/.bashrc (~ is a shortcut to /home/user).

Quote:
Originally Posted by casperdaghost View Post
casper@casperbox /home/casper $ ls -ltr /.bashrc
ls: /.bashrc: No such file or directory
That's because you put a slash in front of it. "/.bashrc" is referencing a .bashrc file located in /, which doesn't exist. If you want to look in your current directory then leave the leading slash off: "ls -ltr .bashrc"
 
Old 06-24-2012, 06:54 AM   #8
jv2112
Member
 
Registered: Jan 2009
Location: New England
Distribution: Arch Linux
Posts: 719

Rep: Reputation: 106Reputation: 106
~usr/.bashrc is a valid location. I mean usr as in jim , larry , jane et cettera. ~ shortcut for /home/Name_of_usr/.bashrc......
 
Old 06-24-2012, 07:23 AM   #9
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 651

Rep: Reputation: 190Reputation: 190
EDIT: this is actually wrong. See below

Quote:
Originally Posted by jv2112 View Post
~usr/.bashrc is a valid location. I mean usr as in jim , larry , jane et cettera. ~ shortcut for /home/Name_of_usr/.bashrc......
It is not, as ~ already contains the username part of the home path.
~usr, issued by the hypothetical user usr, would expand as /home/usrusr, not as the valid location /home/usr.

Last edited by 414N; 06-25-2012 at 04:04 AM. Reason: Wrong assumption
 
Old 06-24-2012, 07:48 AM   #10
jv2112
Member
 
Registered: Jan 2009
Location: New England
Distribution: Arch Linux
Posts: 719

Rep: Reputation: 106Reputation: 106
blah blah blah ...... splitting hairs.... That is what the user would type.............
 
Old 06-24-2012, 08:11 AM   #11
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 651

Rep: Reputation: 190Reputation: 190
EDIT: this is actually wrong. See below
Quote:
Originally Posted by jv2112 View Post
blah blah blah ...... splitting hairs.... That is what the user would type.............
Beg your pardon if I had the "arrogance" of pointing out that the notation you previously used was technically wrong, but I think one should strive to be as correct as possible especially in this section of the forum, where new users come seeking answers to their first Linux questions and would be additionally confused by wrong answers.

Last edited by 414N; 06-25-2012 at 04:05 AM. Reason: Wrong
 
Old 06-24-2012, 11:21 AM   #12
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by jv2112 View Post
blah blah blah ...... splitting hairs.... That is what the user would type.............
I just tested this, and you're right, you can use that syntax, I didn't realize that. I don't know why anybody would, since it's much faster to just use ~, but I suppose if you really wanted to you could.

~/.bashrc, and
~usr/.bashrc

will end up going to the same place.

Learn something new every day.

Last edited by suicidaleggroll; 06-24-2012 at 11:24 AM.
 
Old 06-25-2012, 04:01 AM   #13
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 651

Rep: Reputation: 190Reputation: 190
Very interesting. I've never encountered this syntax so I thought it was wrong.
My sincere apologies to jv2112.
For completeness, here's the relevant portion of the bash manpage:

Code:
   Tilde Expansion
       If a word begins with an unquoted tilde character (`~'), all of the characters  pre‐
       ceding  the  first unquoted slash (or all characters, if there is no unquoted slash)
       are considered a tilde-prefix.  If none of the characters in  the  tilde-prefix  are
       quoted, the characters in the tilde-prefix following the tilde are treated as a pos‐
       sible login name.  If this login name is the null string, the tilde is replaced with
       the  value of the shell parameter HOME.  If HOME is unset, the home directory of the
       user executing the shell is substituted instead.   Otherwise,  the  tilde-prefix  is
       replaced with the home directory associated with the specified login name.
 
Old 06-25-2012, 04:20 AM   #14
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,726

Rep: Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706
Quote:
Originally Posted by suicidaleggroll View Post
I don't know why anybody would, since it's much faster to just use ~,
Because *nix is a multiuser system and ~foo and ~bar can be used to reference the home dir of different users. Just using ~ is good when you are referencing your own home dir, but ~foo and ~bar can be used for others home dirs.

In addition to this there is no requirement that a users home directory has the same name as the user it self. For example the user foo, could have the home directory "/home2/foobar", and ~foo would still reference /home2/foobar". Generally the location of the users home directory is set in /etc/passwd and it is only convention that it is usually /home/username


Evo2.
 
Old 06-25-2012, 09:14 PM   #15
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,415

Rep: Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785
Everything evo2 said; seen it, broke it !
I've even seen a system where all users had the one shared home dir that was also the 'home'/install dir of the APP they used ...
Needless to say, someone managed to delete it ... that was a fun morning...
 
  


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
Need help Trying to open or run a file/program through a c++ program xskycamefalling Programming 4 01-19-2010 03:26 PM
Simple C++ Program: Program Compiles But Won't Run (Segmentation Fault) violagirl23 Programming 3 01-09-2008 01:09 AM
program run from nfs-mounted directory behaves badly jabel5 Other *NIX 3 10-05-2005 09:40 AM
Viewing program messages when program isn't run from command line? Locura Linux - Software 1 09-27-2003 09:19 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:35 AM.

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