Trying to add alias in .bashrc file [duplicate] - bash

This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
Closed 3 years ago.
I need to add permanent alias for bash terminal so that I don't have to set them always after logging in.
I am adding alias command to .bashrc and trying to install the .bashrc file again using the source command.
pxxxxx#pxxxxx:~$ cat .bashrc | tail -2
alias name1='command1'
alias name2='command2'
pxxxxx#pxxxxx:~$
pxxxxxx#pxxxxxx:~$ cat .bashrc | grep export
export EDITOR='vi'
export SHELL= /usr/bin/bash
Now,​ whenever I am logging in into bash or running the following command:
$source .bashrc
I am getting the following error:
pxxxxx#xxxxxx:~$ source .bashrc
bash: export: `/usr/bin/bash': not a valid identifier

I think the issue is with your second export: you say it looks like:
export SHELL= /usr/bin/bash
Try changing it to read:
export SHELL="/usr/bin/bash"
The space after the equals would break setting it as a variable. The quotes aren't necessary, but help stop word splitting.
I can't reproduce the error you're having, but when I try to source a .bashrc with that in there it just fails to return.

Related

Add to $PATH with Shell Script [duplicate]

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.

MacOS .bash_profile - /usr/bin/alias: line 4: builtin: alias: not a shell builtin - how to create an alias?

I have no idea why it isn't working. Using MacOS and bash:
$ echo $SHELL
/bin/bash
Here is the content of the file ```.bash_profile``:
I would imagine that I'm using right file - How do I create a Bash alias? - and the right syntax...
UPDATE:
Tried also .bashrc
When I tried to reload it:
$ source ~/.bashrc
/usr/bin/alias: line 4: builtin: alias: not a shell builtin
UPDATE:
Yes, it looks weird
$ type alias
alias is a shell builtin
$ source ~/.bashrc
/usr/bin/alias: line 4: builtin: alias: not a shell builtin
Given the set of possibilities we've eliminated --
Since you're using bash 3.2, set -x may not be visibly rendering all hidden characters, such as a byte order marker at the beginning of your file.
If the BOM is in fact the problem, then the first line of the file will always cause an error regardless of its contents, and if you move the alias command to the second line, it will then function. Similarly, if you install a more recent version of bash from MacPorts or Homebrew, then running bash -x -l -i will show the hidden characters at the beginning of the command it's trying to run in the generated log.
To solve this, recreate the file in a text editor that doesn't save a BOM, or use "Save As..." and choose a format with no BOM in an editor that gives you the option.
Run the alias command to display all created aliases. Also, you should use ~/.bashrc for aliases, not .bash_profile.
echo "alias p='python -m SimpleHTTPServer'" >> ~/.bashrc
source ~/.bashrc

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.

“export: command not found [duplicate]

This question already has an answer here:
How to restore .bash_profile on a mac? None of my unix terminal are working [closed]
(1 answer)
Closed 2 years ago.
When I open terminal on my mac it shows
Last login: Sun Mar 15 22:12:02 on ttys000
-bash: “export: command not found
-bash: “export: command not found
-bash: “export: command not found
-bash: “export: command not found
(My echo $PATH)
MacBook-Air-Tim:~ timreznik$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/git/bin:/Users/timreznik/bin:/usr/local/bin
MacBook-Air-Tim:~ timreznik$
I have already tried to edit my .bash_profile to
# general path munging
PATH=${PATH}:~/bin
PATH=${PATH}:/usr/local/bin
but it still keep showing me “export: command not found when I launch terminal...
P.S. all commands seems to work but my inner perfectionist is screaming!
First, export is a shell builtin:
$ type export
export is a shell builtin
This means that PATH is irrelevant.
Second, the error message makes clear that the script is attempting to run the command “export. There is no such command:
$ “export
bash: $'\342\200\234export': command not found
The solution is to remove the spurious character from before the string export.
This misspelled command is in one of the shell's initialization files. These would include: ~/.bashrc, /etc/bash.bashrc, ~/.bash_profile, ~/.bash_login, ~/.profile, and any files they include.
Alternatively, the following commands will tell you which files and which lines in those files have the misspelled export command:
PS4='+ $BASH_SOURCE:$LINENO:' BASH_XTRACEFD=7 bash -xlic "" 7>trace.out
grep '“export' trace.out
For details on how the above works, see this post.
I had a similar problem, the culprit was non-breaking space between export and the name of the variable.
To resolve the issue, just replace it with a regular space character.
Details:
I had the following in .bash_profile:
export a=foo
When I start new terminal, I would get
-bash: export a=foo: command not found
If we run xxd on the file, however, we can plainly see the problem (dots are non-printable characters:
$ cat .bash_profile | head -n1 | xxd
00000000: 6578 706f 7274 c2a0 613d 666f 6f export..a=foo
Byte sequence c2a0 stands for non-breaking space

Aliases in .bash_profile not working properly [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I have been trying to alter the .bash_profile that is in my root directory, but have been running into some problems. I am on OS X, Yosemite, on a Macbook Pro. As I understand it, the .bash_profile file contains the script that is called automatically whenever the Terminal app is opened and the bash shell starts.
This is what I currently have written in that file:
PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
export PATH
This works perfectly fine. However, I want to add an alias (right underneath the above two lines) as follows:
alias test='cd ..'
However, when I save this and start up the Terminal, I get the following message:
-bash: alias: ..": not found
Replacing the single quotes with double quotes doesn't help, nor does taking them away altogether. Curiously however, the following alias works:
alias c=clear
When I type c into the terminal, it clear the screen, as you would expect. However, if I instead entered this line with quotes in the bash profile as:
alias c='clear'
Then I will get the following whenever I enter c into the Terminal:
-bash: 'clear': command not found
Note that I do not get an error message on startup for this alias.
What am I doing wrong? Is there a setting I need to change somewhere to get aliases to work properly? I have seen previous examples of aliases and they simply do not work for me.
It looks like shell is not accessing your .bash_profile when logging via terminal.
.bash_profile is a config file of bash shell. When bash shell starting, shell will execute the commands in .bash_profile. But there are many kinds of shells, and different shells execute different config file.
Terminal is a software to receive user input, shell will execute commands.You can use cat /etc/shells to list acceptable shells. For example:
$ cat /etc/shells
/bin/bash
/bin/csh
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh
The default shell is bash shell on Mac OX. But if you have installed zsh, the default shell is zsh, when zsh shell starting, shell will find out the file named .zshrc, and execute the commands in .zshrc.
You can use echo $SHELL to determine the current shell:
$ echo $SHELL
/bin/bash
-> echo $SHELL
/bin/zsh
If your default shell is zsh, .bash_profile don't work. The config file of zsh is .zshrc. And I guess your problem is that your default shell is not bash shell. For example, if your default shell is zsh, you should config the .zshrc , just add
PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
export PATH
or other config to ~/.zshrc.
Then source ~/.zshrc, and the config will work immediately.
Wrapping the command with double quotation worked for me. I was trying with every possible way mentioned in this thread and none of them worked. Then I replaced single quote to double and that worked.
alias mysql_start="sudo /path/to/server/mysql.server start"
For me, it didn't work in the same terminal. I had to open a new one to get it work.
The killer for me was space and single quotes.
alias test="cd .." worked.
Don't put any spaces between alias_name=
User level 'system' files need to contain 'plain text'. How to set/configure this for your 'editor-of-choice' can vary (I don't use a Mac so I'm not much help with that.) Soo,
create your profile 'from the shell' by appending lines directly, i.e. remove the 'bad lines and then:
echo "some command string" >> ~/path/bash_profile
use 'vi' or 'vim' ## should be 'safe'
review your file via:
cat -v ~/path/bash_profle | more ## see any 'funny chars'? or
cat -ve ~/path/bash_profle | more ## see any 'funny chars'? or
or
vi ~/path/bash_profle # then set 'control codes' to 'on'
set list ## see any 'funny chars' for your 'quotes'?
What I realised is that Mac has option for smart quotes and dashes.
alias ..='cd ../' is different from alias ..=‘cd ../‘, where the former works but the latter doesn't.
You can run alias custom auto startup in ~/.bash_profile or ~/.alias_file... by paste file name alias in ~/.bashrc if you use bash or ~/.zshrc if you use zsh.
Ex:
if [ -s ~/.bash_profile ]; then
source ~/.bash_profile;
fi
I fixed this by editing my .bash_profile in vim or nano something about text edited messed it up unsure why.
I tried to edit in in notepad just using "open .bash_profile" however something about the symbols translated wrong. So you have to either "vim .bash_profile" or "nano .bash_profile"
Doing this corrected this problem for me.
Make sure that the alias commands are together in the .bash_profile too. That is what was wrong with mine.
Also, if you just type in alias in the terminal, it should list all the known alias commands, so if you don't see your command you know something is wrong.
If alias is not recognizing, first identify what shell you're using when you open up your terminal or commandlineprompt echo $SHELL. For me it's /bin/bash so i'll input my aliases in ~/.bash_profile
Take note, the file ~/.bash_profile can store both aliases to call out during a terminal session and autorun those same aliases upon opening up a terminal session. The example below would be to open up a file to edit by text via vim-software. You may change it other text-editors as you wish. You can see the difference by closure-method.
ALIAS CALLOUT (uses parenthesis)
alias editbp="vim ~/.bash_profile"
ALIAS AUTORUN (uses tilda)
alias editbp=`vim ~/.bash_profile`

Resources