Adding to path so that file is available everywhere - bash
-
13-07-2021 - |
質問
I am trying to add several directories to my path so that the files in that directory and it's subdirectories are available for me from the command prompt across sessions.
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/home/username/OpenFOAM/OpenFOAM-2.1.0/etc/bashrc:/usr/share/texmf-texlive/tex/latex:/etc/crontab:/home/username/Research/Dissertation/wigner/ic/L=lambda:/home/username/Research/Dissertation/wigner/ic/L=2lambda:/home/username/Research/Dissertation/wigner/ic/L=3lambda:/home/username/Research/Dissertation/wigner/ic
Appending the follwing
/home/username/Research/Dissertation/wigner/ic/L=lambda:/home/username/Research/Dissertation/wigner/ic/L=2lambda:/home/username/Research/Dissertation/wigner/ic/L=3lambda:/home/username/Research/Dissertation/wigner/ic
to the export PATH
line of my .bashrc
and sourcing it didn't help. What am I doing wrong? When I try to access files from say /home/username/Research/Dissertation/wigner/ic
, I can't.
I've read a few posts here but they didn't really help at all. Am I missing colons, semicolons, dollar symbols?
解決
It sounds like you might be misunderstanding the purpose of $PATH
. $PATH
merely tells Bash where to look for executable files and scripts. For example, this command:
foo bar.txt
will (usually) search $PATH
for an executable file named foo
, and these commands:
bash foo.sh
. foo.sh
will (usually) search $PATH
for foo.sh
unless there's a foo.sh
in the current directory; but these commands:
cat foo.txt
vi foo.txt
less foo.txt
will not search $PATH
for foo.txt
.
Furthermore, you write of "the files in that directory and it's subdirectories", but $PATH
is not useful for subdirectories. Bash will never search $PATH
if the executable-name contains /
. For example, this command:
foo/bar baz.txt
will run ./foo/bar
, and will not search $PATH
for a directory named foo
.
Edited to add: So, what you can do instead . . .
Ultimately, you need to include directory information in your montage
command:
cd /home/username/Research/Dissertation/wigner/ic
montage -geometry +4+4 L=3lambda/three.jpg L=2lambda/two.png output.jpg
If the directory information is too much of a pain to type every time, you can set up your own variable in .bashrc
, and then use it explicitly. For example, .bashrc
might have:
export IC=/home/username/Research/Dissertation/wigner/ic
export ICL=$IC/L=lambda
export ICL2=$IC/L=lambda2
export ICL3=$IC/L=lambda3
and then you could write:
montage -geometry +4+4 $ICL3/three.jpg $ICL2/two.png output.jpg
If you don't even want to remember what subdirectory a given file is in, you can use a fileglob:
export IC=/home/username/Research/Dissertation/wigner/ic
export ICLs=$IC/L=lambda*
and write:
montage -geometry +4+4 $ICLs/three.jpg $ICLs/two.png output.jpg
letting the shell find it for you. (But of course, this will only work properly if there are no name-conflicts between files in different subdirectores.)
他のヒント
You could define a few variables in your .bashrc
:
H="/home/username/Research/Dissertation/wigner/ic"
oneLam="$H/L=lambda"
twoLam="$H/L=2lambda"
threeLam="$H/L=3lambda"
so that referring to files can be done from anywhere like so:
user@box:/some/horrendously/deep/path/$ vi $twoLam/some_file.txt