Alias not working in Ubuntu 14.04 [duplicate] - bash

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.

Related

Alias stops working after adding another one [duplicate]

This question already has answers here:
Are shell scripts sensitive to encoding and line endings?
(14 answers)
Closed 2 years ago.
I am using WSL2 Ubuntu with Windows. I have an alias opening Notepad++ on Windows defined inside WSL like this:
alias npp="/mnt/c/Program\ Files/Notepad++/notepad++.exe"
and it works... but as soon as I add another alias, e.g.
alias npp="/mnt/c/Program\ Files/Notepad++/notepad++.exe"
alias l="ls -l"
I get the error - : No such file or directory/Notepad++/notepad++.exe. It works only when it is the last alias defined in the file.
My guess is it might have something to do with a space in the Notepad++ path but I am not really sure anymore. I tried different ways of escaping with no luck. What is happening here, what's the cause and the fix?
I can reproduce if I edit a script with those aliases using Notepad++ and leave the line endings as "Windows". If you want to use Notepad++ to edit your bash scripts, make sure to use Edit -> EOL Conversion -> Unix (LF) before saving.
After doing that, the alias script works correctly for me.

bash alias disappears when opening new terminal [duplicate]

This question already has answers here:
Should aliases go in .bashrc or .bash_profile? [duplicate]
(4 answers)
Closed 4 years ago.
im setting up a new alias by typing this command:
vi ~/.bashrc
and then placing my alias:
alias school='ssh -Y username#linux.student.cs.uwaterloo.ca'
followed by exiting the file using: wq
however when i close my terminal and open my terminal, i get a "command can't be found." error message.
if i type source ~/.bash_aliases, it will work, the alias will work, but when i open a new terminal it won't.
is my .bashrc supposed to be empty when i vi into it?
The reason your alias is getting lost is because you dont have your bashrc sourced in a new terminal.
Same will happen even if you create a new alias file and source it in bashrc because its scope gets limited to the terminal you are editing in.
What you can do is logout once and then log in back so that bashrc entries gets updated for your user account or you can source in each terminal by typing
source ~/.bashrc
By adding the same entry to '''.profile''' you are making sure the alias is set on each system boot.
So its better to set the alias in .bashrc rather than .profile
Another major point to nite here is to make sure you dont delete anything in bashrc since that will do catastrophic changes to you session.

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

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.

Bash error after opening Terminal and running shell scripts (CentOS) [duplicate]

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.

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