Add a bash script to path
Try this:
- Save the script as
apt-proxy
(without the.sh
extension) in some directory, like~/bin
. - Add
~/bin
to yourPATH
, typingexport PATH=$PATH:~/bin
- If you need it permanently, add that last line in your
~/.bashrc
. If you're usingzsh
, then add it to~/.zshrc
instead. - Then you can just run
apt-proxy
with your arguments and it will run anywhere.
Note that if you export
the PATH variable in a specific window it won't update in other bash instances.
How to set PATH only in bash script temporarily?
it's simply not true.
If you write a script and change the $PATH variable, the change live only in the script:
vi test.sh
inside the file:
#!/bin/bash
export PATH="$PATH:test"
let's test:
echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/home/matteo/.local/bin:/home/matteo/bin:./bin:/home/matteo/.local/bin:/home/matteo/bin:./bin
chmod ug+x test
./test
echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/home/matteo/.local/bin:/home/matteo/bin:./bin:/home/matteo/.local/bin:/home/matteo/bin:./bin
same output. The change is effective only inside the script!
Add to $PATH with Shell Script
Let's consult man bash
:
export [-fn] [name[=word]] ...
export -p
The supplied names are marked for automatic export to the envi-
ronment of subsequently executed commands.
Note "subsequently executed commands", therefore the effect of your script ends, once the script ends.
Bash Script To Add A Folder/Directory to the path in linux not working
First thing - you should use echo $PATH
. By simply typing $PATH
you're trying to execute the command, hence the "No such file or directory error"
Next - the /root/My_Scripts/Bash_Scripts
wasn't really added to the PATH
. The first output you see in done inside the script, so the changes could be seen there.
The reason is that PATH
will be set only in the context of the script shell, execute it as source add_path
to preserve the changes in variables (but only for current shell).
If you want the variable to be persistant in all shells - add it to /.bashrc
(since you're runnung as root).
Bash script is working only in path where is bash script file
The problem is that you are using ls
after cd
to a particular directory. The output of ls
is just a file name without a path. Later you pass that file name without path to the stat
command. If your current directory is different, then stat
won't find the file.
Possible solutions:
Add the directory (
dir
) to thestat
commanddir='/path_where_is_test.sh_file'
file=$(cd "$dir" && ls -t | head -1)
last_modified=$(stat -c %Y "$dir/$file")Use the changed directory
last_modified=$(cd '/path_where_is_test.sh_file' && stat -c %Y $(ls -t | head -1))
How to run a .sh-script from any path in a terminal?
One option is simply to type the path to the script:
~/Desktop/script
This works fine, but gets a bit unwieldy.
This is what the PATH
environment variable is for. And it is what $HOME/bin
is for.
- Create yourself a directory
$HOME/bin
. Put all your executable scripts in it (make them executable withchmod +x script
if need be††). This way, there's one place to look for the scripts you want to run. - Add
$HOME/bin
to yourPATH
. I put mine at the front:PATH="$HOME/bin:$PATH
, but you could put it at the back if you prefer. - Update your
.profile
or.bash_profile
(or possibly.bashrc
) file to setPATH
. Beware of a continually growing PATH, though.
As tripleee noted, once the command is installed in a directory on PATH
, you no longer type ./script
, but just script
. This is exactly like you type ls
and not /bin/ls
, etc. Once the program is installed in a directory on your PATH
, it is (for many purposes) indistinguishable from a system-provided command.
I have about 500 scripts and programs in my $HOME/bin
directory.
Note that this doesn't require any special privileges. If you have administrator access to your machine and you think other users might find your commands useful, then you could install the scripts/programs in one of the system-provided directories on your PATH
. However, it is usually best not to add programs to any of:
/bin
/usr/bin
/sbin
/usr/sbin
There is often/usually /usr/local/bin
which is a suitable place for widely used commands not provided by the system.
†† It would be better to use chmod a+x,go-w script
; your scripts should not be writable by other people. You could even simply use chmod 555 script
or chmod 755 script
. I tend to keep my scripts non-writable. That way, I have to go through a formal change process with the version control system. It means there's less danger of uncontrolled changes.
Bash:script cannot be called after adding the path
Tilde doesn't get expanded inside strings. So by quoting the right-hand side of the assignment you prevent it from being expanded and get a literal ~
in your PATH
variable which doesn't help you any.
You have two ways to fix this:
Drop the quotes on the assignment (yes this is safe, even for
$PATH
values with spaces, etc.).Use
$HOME
instead of~
.
I prefer the second solution but the first is entirely valid for this case.
Beware though that in places where you aren't doing a straight assignment you often cannot just drop the quotes and trying to use ~
will cause problems.
In which case you will end up finding a question like this with an answer like this and something ugly like this.
how to escape file path in bash script variable
With GNU bash and its Parameter Expansion:
echo "${CONFIG//\//\\/}"
Output:
\/home\/teams\/blabla\/blabla.yaml
Related Topics
How Do Applications Resolve to Different Versions of Shared Libraries at Run Time
Converting Jiffies to Milli Seconds
How to Attach a File Using Mail Command on Linux
Bash Capturing Output of Awk into Array
Why Do We Need a Bootloader in an Embedded Device
Append Date to Filename in Linux
Do I Need -D_Reentrant with -Pthreads
Best Way to Find Os Name and Version in Unix/Linux Platform
Is /Usr/Local/Lib Searched for Shared Libraries
Run Script with Rc.Local: Script Works, But Not at Boot
How to Get the Name of the Current Git Branch into a Variable in a Shell Script
Using 'Date' Command to Get Previous, Current and Next Month
Find a Pattern in Files and Rename Them
How to Change Bash History Completion to Complete What's Already on the Line
Shebang Line Limit in Bash and Linux Kernel
Difference Between Shell and Environment Variables
Variable in Bash Script That Keeps It Value from the Last Time Running