This question already has answers here:
Why can't I change directories using "cd" in a script?
(33 answers)
Closed 5 years ago.
As a beginner of bash script, I wrote a simple script to change the directory. Here it is my source code:
#!/bin/bash
set -x
echo "---------------------START-----------"
cd /home/cocadas/Workspace/carnd/CarND-Behavioral-Cloning-P3
I save it as "start" in the /root folder. I change the property of the file to be executable, and then run it as below. The problem is that the execution command cd doesn't work. What did I miss?
cocadas#cocadas-ThinkPad-W540:~$ ./start
+ echo ---------------------START-----------
---------------------START-----------
+ cd /home/cocadas/Workspace/carnd/CarND-Behavioral-Cloning-P3 cocadas#cocadas-ThinkPad-W540:~$ cd
/home/cocadas/Workspace/carnd/CarND-Behavioral-Cloning-P3
cocadas#cocadas-ThinkPad-W540:~/Workspace/carnd/CarND-Behavioral-Cloning-P3$
Your ./start call creates a sub shell. Run source start or . start (. is an abbreviation of source) instead to execute your script directly in your command line, not in a nested container.
Related
This question already has answers here:
Shell script current directory?
(7 answers)
Closed 2 years ago.
I want to be able to execute a bash script located in my home directory with the command ~/script_name.sh from any directory and then in the script, get the directory that it was ran from.
eg. I'm in the directory /foo/bar/baz, and execute /foo/script.sh, and it prints out /foo/bar/baz
pwd and $0/$BASH_SOURCE give me the directory my shell starts in and the path to the script in my home directory respectively.
Instead of invoking the external command pwd, consider using $PWD instead. If you want to protect against some rogue code having explicitly changed PWD, do a cd . (which is an internal command) first, which restores PWD to the correct value.
This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
Run bash commands from txt file
(4 answers)
Closed 5 years ago.
I have a script shell that is reading from a file. This is the command line text that i would like to run under the bin folder of neo4j
bin/neo4j-import --into /home/micmal/neo4j-community-3.0.1/data/databases/graph_test.db --id-type string --nodes:
I would like to use a script shell to get that command and go to the folder of neo4j and paste it so it runs.
my shell looks like :
#!/bin/bash
batch_import_value= `cat _batchfile.txt`
cd /home/neo4j-community-3.0.1/
echo $batch_import_value`
it doesn't seem work
Any idea?
This question already has answers here:
How do I get the directory where a Bash script is located from within the script itself?
(74 answers)
Closed 5 years ago.
I have a script file, let's say abc.sh. The full path is /home/path/to/script/abc.sh. Now I am currently executing the script from /path/to/current by executing this command -
sh /home/path/to/script/abc.sh. I want this script to echo it's location. The output should be /home/path/to/script
Use this script.
#!/bin/bash
current_location=$(pwd)
script_location=$(dirname $0)
if [ $script_location= '.' ]
then
script_location="$current_location"
fi
echo $script_location
This question already has answers here:
Why can't I change directories using "cd" in a script?
(33 answers)
Closed 5 years ago.
I have this script in a file called /Users/tcl/scripts/gotoroot:
echo "hello"
cd /
echo "good bye"
But when I run it I get this:
User:scripts tcl$ pwd
/Users/tcl/scripts
User:scripts tcl$ gotoroot
hello
good bye
User:scripts tcl$ pwd
/Users/tcl/scripts
User:scripts tcl$
The directory has not changed and I don't know why? It should be /, not /Users/tcl/scripts
When you run the script, it starts a subshell in a new process. The cd changes directory inside that subshell, not inside your terminal process.
Test it out by putting this in your script:
pwd
cd /
pwd
You should see that it changes to / inside the script.
Shell scripts are run inside of sub processes. The CD is working but it's happening inside a different process than your main terminal session and has no effect on the working directory once you return to your terminal session.
This question already has answers here:
Why do you need ./ (dot-slash) before executable or script name to run it in bash?
(9 answers)
Closed 5 years ago.
When I executeinit-hooks I get
bash: init-hooks: command-not found
here are the contents of init-hooks:
#!/bin/bash
set -e
printf '\ncopying hooks\n\n'
cp ./hooks/* ../../.git/hooks
When I execute cp ./hooks/* ../../.git/hooks from bash directly execution is successful.
(note this is the same command as what is in the script)
Proof of the files are in the directory and the results of execution:
Why does my script behave differently than the command/why is my script not found?
On the Linux systems (where bash comes from) the current directory is usually not included in the path for security reasons.
Run echo $PATH to check what directories are used to search for executables when they are provided in the command line without a path. The current directory (.) should not be there.
Run the script as ./init-hooks and bash will find it.
I suugest to run it following way
./init_hooks
or put fully qualified file name.
make sure to make the script executable
chmod +x ./init_hooks