Why can't I execute this simple shell script? [duplicate] - macos

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.

Related

Problem in executing user defining shell commmand

I am trying to learn the basics of shell. I used vim editro for creating my own list of commands to be executed. Here is the way I created the code
vi mycommands
then inside this file I wrote
cd Documents
I am using macOS Catalina which has zsh by default but switched to bash
So when I write the following command in the terminal:
$ sh +x mycommands
It shows
+cd Documents
The Documents has some files and directories but it is not changing directory.Where am I going wrong?
Any help will be greatly appreciated.
Scripts run like sh myscript execute in a separate sub-shell, not the current shell. Changing directory inside a script will not cause your shell to change directory. If you want to change directory in your shell, you need to run the commands in your shell.
To do that, run:
. ./myscript (sh, bash) or source ./myscript (bash).
See this question.

Not able to change directory from bash script despite sourcing [duplicate]

This question already has answers here:
Why can't I change directories using "cd" in a script?
(33 answers)
Closed 2 years ago.
I have following 2 files:
file cd2vcaa (in path):
#! /bin/bash
cd /var/cache/apt/archives
file test.sh (in current directory):
#! /bin/bash
. cd2vcaa
From terminal, I am able to change directory with . cd2vcaa but not with ./test.sh
~$ cd2vcaa <-- no effect
~$ . cd2vcaa <-- changes directory
/var/cache/apt/archives$ cd <-- back to home directory
~$ ./test.sh <-- does not change directory though no error - why?
~$
Why is . cd2vcaa working from terminal but from within another script?
How can this problem be solved?
The test.sh runs in a separate shell when you invoke it using ./test.sh, so although the sourcing inside test.sh means that the cd command will affect the current directory in the shell in which test.sh runs, so for example any commands added at the end of test.sh would run with current directory /var/cache/apt/archives, it will not affect the parent shell (your login session).
If you invoke the test.sh by sourcing it (. test.sh), then no sub-shell is launched at either stage, and the directory is changed in your login session.
As discussed in the comments, if the aim is simply to have use of an interactive shell which is cd-ed to the specified directory (not necessarily the parent shell), then one option is just to put bash at the end of the test.sh in order to start an interactive subshell after the cd command has been run. On exit from this subshell, control will return to the login shell, still in its original working directory.

Alias not working in Ubuntu 14.04 [duplicate]

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.

Creating aliases in .bash_profile that run a shell script

So I have a script called spotlyrics.sh that I want to be able to run using the command "lyrics" in the terminal.
I have opened up my .bash_profile and am wondering how I can create the alis which 1) finds the script and then 2) executes it
The file is inside a folder called bash at the following path
/Users/username/Documents/bash
What I have so far (inside my bash profile), which doesn't work because I guess it's not "executing" the script.
alias spotlyrics=“/Users/username/Documents/bash/spotlyrics.sh“
I get the following error when running "spotlyrics" in the terminal:
-bash: “/Users/username/Documents/bash/spotlyrics.sh“: No such file or directory
Would love some help, thanks!
You've been editing your .bash_profile with something that is not a proper text editor. The quotation marks are not ASCII, and therefore not actually quotation marks as far as the shell is concerned.
Instead of beating around the bush with aliasing a script to a name it mostly already has, why not put the script in a directory in PATH and let it be its own command?
mkdir ~/bin
echo 'PATH+=:$HOME/bin' >> ~/.bashrc
mv "/path/to/spotlyrics.sh" ~/bin/spotlyrics && chmod +x ~/bin/spotlyrics
Then restart the shell (log out and back in) and you won't need the alias.
Well, the shell scripts are not executable by just calling it's name, they should be run using "source" command(in case of not c-shell, dot command(.) can also be used).So while adding an alias in .bashrc or .bash_profile for running a shell script append source command before the path to the shell script.
In your case probably this should work:`
alias spotlyrics='source /Users/username/Documents/bash/spotlyrics.sh'`
Please let me know if it doesn't work. Because it worked for me.

Export shell directory to terminal [duplicate]

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.

Resources