This question already has answers here:
Why can't I change directories using "cd" in a script?
(33 answers)
Closed 7 years ago.
I'm creating a shell script that will allow a back functionality on my shell. What would be a good way to change the terminal's directory in the script.
Just running something like this in my back.sh file doesn't work:
cd /
Even when I run the file like
source ./back.sh
A sample file that is not working properly:
#!/bin/bash
cd ~/Movies/
when I run source ./back.sh or source back.sh
Here are my 2 cents about the difference between . and source builtins: There isn't any.
Since you mentioned that you use . for cd ../, make sure that the proper . and source are executed and are really the same thing:
user#host> type source
source is a shell builtin
user#host> type .
. is a shell builtin
Other than that /bin/bash --version may turn out helpful, too.
Related
This question already has answers here:
How to run 'cd' in shell script and stay there after script finishes?
(7 answers)
Closed last year.
Trying to make a script to automate activating a virtual environment for my Django project. I am running 'BIT BASH' on the windows 10 operating system. I have a feeling Windows 10 may be where my issues lie. Could anyone confirm this?
Here is my script which I save as activatevirt.sh.
I run it by starting up the 'bit bash' command line program. Then to run the script,
I enter the line:
sh activatevirt.sh
Commands such as echo work, but for some reason file system navigation commands are what seem to fall apart. This is why I suspect the issue to be the fact I'm doing this in windows 10 whereas others trying to offer help may be running on a Unix system. Please consider this if offering a solution.
If someone could simply give a simple brief bit of code that does nothing more than to change the directory in Bit Bash using a .sh script, I would be very grateful as I need to automate much more than just this, but this is where I am currently stuck. Below here is the code which doesn't work atm!
cd c:
cd virt
cd scripts
source activate
whoami
Errors which show up: line 1 no such file or directory c:
I think you mean "git" bash? Try /C, or possibly /mnt/c, instead of c:.
You can use a single path too:
cd /C/virt/scripts
Or just source it without changing directory:
source /C/virt/scripts/activate
Note unix style (forward) slashes to separate directories, as opposed to Microsoft style backslashes (\).
There's also sh /C/virt/scripts/activatevirt.sh for example.
This question already has answers here:
How to permanently set $PATH on Linux/Unix [closed]
(24 answers)
Closed 4 years ago.
I want to add PATH on Linux Ubuntu 18.04 from file path.sh , my file include :
#!/bin/bash
export PATH="$PATH:/root/.local/bin"
chmod+x path.sh , but when i run it ./path.sh Path not add when i type echo $PATH .
but when i type in terminal export PATH="$PATH:/root/.local/bin" it added to my PATH .
Am I miss something on my file?
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.
source path.sh
add this step is fine.
This question already has answers here:
How to set an alias inside a bash shell script so that is it visible from the outside? [duplicate]
(4 answers)
How to reload .bashrc settings without logging out and back in again?
(18 answers)
Closed 6 years ago.
So I have added an alias for jumping to another directory like this
in my ./bashrc file , Looks like this
alias crmx="cd /var/www/crm/website-crm/"
Then i saved the file
but when I try to run
crmx
it says
command not found
Also I tried to do alias to see all the command but my command is not listed
Any idea ?
The file is ~/.bashrc (starting with a dot).
And you have to source it (reload) by doing source ~/.bashrc or just by closing and reopening your terminal.
You can also type ps to check if your shell is bash (for example if it's zsh you have put your alias in the ~/.zshrc file)
Let's suppose you are on bash.
Once you saved that file, you have to have bash read it with a command like this:
. ~/myaliasfile
or like this
source ~/myaliasfile
if the file resides in your home directory. Specify the path (relative to your home or absolute) otherwise.
Then you'd go to your .bashrc file and add the very same line to the end of it. By doing so the alias(es) will be read and made available to every single bash invocation and login.
Done!
More details here and here.
This question already has answers here:
Why can't I change directories using "cd" in a script?
(33 answers)
Closed 9 years ago.
I have a file called go in ~/
All I want to do is to be able to run go and have it cd to this other dir.
This is what the go file looks like:
$ cat go
#!/bin/bash
cd ~/Desktop/rs3
I ran the line $ chmod +x go
And then the line ./go to attempt to run the file.
If i put echo whatever in the file it will print whatever to the console, but the cd command never works.
Thanks.
It doesn't work because the script runs in a subshell, so the environment is different.
What you can do is to alias go='cd ~/Desktop/3s3' - as it's an alias, the shell performs the substitution and runs the cd on itself, as if you've just typed it.
You should define the alias in your ~/.bashrc, ~/.bash_profile or any file that gets sourced when you login.
This question already has answers here:
Error message on Terminal launch [duplicate]
(2 answers)
Closed 6 years ago.
Every time I open Terminal in CentOS 6.4, I get the error:
bash: usr/local/bin: No such file or directory
I've checked .bashrc and .bash_profile to see if there are any lines that reference usr/local/bin, but haven't found anything. The same error also appears when I switch to root, or run a shell script.
Is it as simple as adding a backslash in front of usr? Like so--
/usr/local/bin
Still don't know where the error is happening though. Any help is much appreciated. Thanks!
This is strange as the normal bash directory on a centos 6.4 system is /bin/bash, however I would advise you to check the following:
echo $SHELL
It should pull your SHELL environment variable to show you where what shell you are using, normally it looks like this:
SHELL=/bin/bash
If it's different say for example:
SHELL=usr/local/bin/bash
then I would check your passwd file to make sure your users default shell is pointing to the right place.
username:x:601:601::/home/username:/bin/bash
Also I would suggest check where you shell actually lives
which bash
/bin/bash
And make sure everything is pointing to the correct location.