Source files in a bash script
Execute Shell Script Using . ./ (dot space dot slash)
While executing the shell script using “dot space dot slash”
, as shown below, it will execute the script in the current shell without forking a sub shell.
$ . ./setup.bash
In other words, this executes the commands specified in the setup.bash
in the current shell, and prepares the environment for you.
Unable to source a simple bash script
Okay. After 10 months, my wife helped me debug this problem.
I had an alias in .bash_profile alias fi='firebase init'
Alias name fi makes bash to confuse with if statement.
Sourcing the source files using bash script
By using dirname
you get the directory of where the script is located, therefore it's easy to source other files locally close to your script and don't worry about specifying the correct path each time the script bundle is relocated.
For instance if you have in your script source $macros/some_script.sh
then it will not break when the bundle is located in the /usr/local/bin/
or /bin/
or ...
Regarding $BASH_SOURCE
see: https://stackoverflow.com/a/35006505/2146346
How best to include other scripts?
I tend to make my scripts all be relative to one another.
That way I can use dirname:
#!/bin/sh
my_dir="$(dirname "$0")"
"$my_dir/other_script.sh"
How to source a script in a Makefile?
To answer the question as asked: you can't.
The basic issue is that a child process can not alter the parent's environment. The shell gets around this by not forking a new process when source
'ing, but just running those commands in the current incarnation of the shell. That works fine, but make
is not /bin/sh
(or whatever shell your script is for) and does not understand that language (aside from the bits they have in common).
Chris Dodd and Foo Bah have addressed one possible workaround, so I'll suggest another (assuming you are running GNU make): post-process the shell script into make compatible text and include the result:
shell-variable-setter.make: shell-varaible-setter.sh
postprocess.py @^
# ...
else
include shell-variable-setter.make
endif
messy details left as an exercise.
How to include file in a bash shell script
Simply put inside your script :
source FILE
Or
. FILE # POSIX compliant
$ LANG=C help source
source: source filename [arguments]
Execute commands from a file in the current shell.
Read and execute commands from FILENAME in the current shell. The
entries in $PATH are used to find the directory containing FILENAME.
If any ARGUMENTS are supplied, they become the positional parameters
when FILENAME is executed.
Exit Status:
Returns the status of the last command executed in FILENAME; fails if
FILENAME cannot be read.
How do I run a shell script without using sh or bash commands?
Add a "shebang" at the top of your file:
#!/bin/bash
And make your file executable (chmod +x script.sh
).
Finally, modify your path to add the directory where your script is located:
export PATH=$PATH:/appropriate/directory
(typically, you want $HOME/bin
for storing your own scripts)
Reliable way for a Bash script to get the full path to itself
Here's what I've come up with (edit: plus some tweaks provided by sfstewman, levigroker, Kyle Strand, and Rob Kennedy), that seems to mostly fit my "better" criteria:
SCRIPTPATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
That SCRIPTPATH
line seems particularly roundabout, but we need it rather than SCRIPTPATH=`pwd`
in order to properly handle spaces and symlinks.
The inclusion of output redirection (>/dev/null 2>&1
) handles the rare(?) case where cd
might produce output that would interfere with the surrounding $( ... )
capture. (Such as cd
being overridden to also ls
a directory after switching to it.)
Note also that esoteric situations, such as executing a script that isn't coming from a file in an accessible file system at all (which is perfectly possible), is not catered to there (or in any of the other answers I've seen).
The --
after cd
and before "$0"
are in case the directory starts with a -
.
Related Topics
Run Meteor as a Daemon Process
How to Lock a Directory in C on a Linux Machine
How to Decide How Much Stack I Can Use After a Call to Pthread_Attr_Setstacksize
Cuda - Confusion About The Visual Profiler Results of "Branch" and "Divergent Branch" (2)
How to Get Jenkins Working with Binaries from a Subfolder of The Root User
Splitting a Large Directory into Smaller Ones in Linux
Various Docker Container Paths Have Started Failing with Permission Errors on Linux Mint
Linux - Bash Redirect a String to a File
Giving Linux User Git Access But Not Shell Access
Bash Script Commands Not Working in Cron
Ssh - Help Understanding Proxy Command
Cannot Kill Redis-Server on Linux
How to Simulate a Usb Printer to Lpt on Linux
Range: Bytes 0- Fails in Apache 2.2.23
Insecure $Env{Path} While Running with - T Switch
Vue Npm Run Serve Failed to Load Resource: Net::Err_Content_Length_Mismatch