mac how to save alias in computer - linux

I created an alias on terminal, such as
alias hw="cd Desktop/2015hw"
but after I closed the terminal and do hw, there is an error message said there is no hw command.
I was wondering if we can save the alias on the computer.
Also, if we have stored the alias on the computer, how would we check it? By check it, I mean like list all alias we have stored.
Thanks.

Add the command to your .bashrc file
echo "alias hw='cd Desktop/2015hw'" >> ~/.bashrc
Keep in mind that your alias will only work when you are in your home (as you are using a relative path)
echo "alias hw='cd ~/Desktop/2015hw'" >> ~/.bashrc
Execute the saved alias by sourcing the file
source ~/.bashrc

When making an alias with alias, the alias is only valid until you close the terminal window. If you open another terminal window, the alias will no longer be present.
You can make an alias be valid for all terminal windows by placing it in your ~/.bash_profile or ~/.bashrc files (bash is the name of the terminal which Mac OS X ships with by default.)
Since Mac OS X does not load .bashrc by default, I would actually do:
echo "alias hw='cd ~/Desktop/2015hw'" >> ~/.bash_profile
When you want to delete the alias, you can open your ~/.bash_profile or ~/.bashrc files and delete it manually.
To open them, do open -a TextEdit ~/.bash_profile or open -a TextEdit ~/.bashrc
Finally, to list all your current aliases, simply type alias in Terminal.

your can start with type
ls -la
and then if you use bash typing
nano .bash_profile
or if you use zsh typing
nano .zshrc
then make alias save with ctrl + o
for activating
use
source ~/.bash_profile or
source ~/.zsh

Related

Alias not updated after using source .bashrc

I am testing the bash behavior on login (terminal 1), but I got confused about its interaction with alias:
I opened with vim .bashrc and add this line :
alias ls='ls -l'
and save it with :x
then I used source .bashrc to simulate a new login session and I found it in the aliases list
But I removed the alias from .bashrc and use source .bashrc again I saw that alias ls='ls -l' was still available. On the other hand, opening new shell terminal (terminal 2) the problem was solved.
Question: Why alias ls='ls -l' was not removed on the first terminal ?
Sourcing .bashrc doesn't clear what you have defined so far. It just adds the definitions it contains to your current environment.
If you want to undefine a given alias, just type:
$ unalias ls
$ source .bashrc
If you want to undefine all aliases:
$ unalias -a
$ source .bashrc
Finally, if you want to start over with a brand new shell, you can of course close your session and reopen one, but here is an almost identical command in case this is not that easy (ssh) or undesirable:
$ exec bash
(you may also add the -l option to simulate a login shell, thus reading your ~/.bash_profile file)

why .bashrc not executed when open a new terminal?

According to the bash startup description, .bashrc should be reloaded when doing a non-login interactive shell.
When an interactive shell that is not a login shell is started, Bash reads and executes commands from ~/.bashrc, if that file exists.
I append a new variable into .bashrc like NAME="TEST_BASHRC" then save it.
But when open a new terminal, the variable $NAME is still None. That is to say, .bashrc isn't reloaded when opening new terminal.
Why .bashrc not executed and how can I make .bashrc reload when opening new terminal?
The following is what I do:
# comment: append variable "NAME" to .bashrc
$ cat .bashrc
export NAME="TEST_BASHRC"
# comment: open a new terminal then...
$ echo $NAME
$
You need to export the variable:
Try:
export NAME="TEST_BASHRC"
See why here

I set .bashrc but my promt looks still the same

i'm quite new with linux and I want to tweak my terminal prompt. The default is user#user - Thinkpad- T420 ~$. That pretty annoying long.
I searched google and I tried my best but it didn't show any difference.
What i have done:
echo $PS1
\[\e]0;\u#\h \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u#\h\[\033[00m\] \[\033[01;34m\]\w \$\[\033[00m\]
locate bashrc
/etc/bash.bashrc
/etc/skel/#.bashrc#
/etc/skel/.bashrc
/etc/skel/.bashrc~
/usr/share/base-files/dot.bashrc
/usr/share/doc/adduser/examples/adduser.local.conf.examples/bash.bashrc
/usr/share/doc/adduser/examples/adduser.local.conf.examples/skel/dot.bashrc
sudo -s Atom /directory
I changed in all files PS1="...." to PS1="hallo". Terminal prompt shows no difference.
Linux Kernel
Mint
4.8.0-53-generic
greetings ! B
source ~/.bashrc
Your terminal only reads the .bashrc on opening, or when you explicitly source it.
I fixed it by editing the .bashrc file in my /home/myusername directory. Don't edit the files here /src/skel

linux: how to execute profile file [duplicate]

This question already has answers here:
How to reload .bashrc settings without logging out and back in again?
(18 answers)
Closed 3 years ago.
My colleague gave me a file containing lots of configs such as
alias ll="ls -l"
alias lr="ls -lrt"
alias gv="vim -g"
How can I use(execute) this profile?
You can load the profile using source command:
source <profile-filename>
eg:
source ~/.bash_profile
For your custom aliases, the best place should be at ~/.bash_aliases. You must just be sure the ~/.bashrc file already contains the following lines:
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
or in a more concise manner:
[ -f ~/.bash_aliases ] && . ~/.bash_aliases
To load them immediately, source it. Otherwise, aliases will be loaded at every terminal opening. To check, use the alias command without argument.
You can put it in your local .bashrc (or appropriate shell rc file) file if you want it permanently.
cat fileNameProfile >> ~/.bashrc
And for current session:
source ~/.bashrc
If you want it just now, then:
source fileNameProfile
For one time use, copy paste commands to your terminal:-
alias ll="ls -l"
alias lr="ls -lrt"
alias gv="vim -g"
For everytime use, add it in .bashrc.
echo 'alias ll="ls -l"' >> ~/.bashrc
echo 'alias lr="ls -lrt"' >> ~/.bashrc
echo 'alias gv="vim -g"' >> ~/.bashrc
and then source ~/.bashrc
To autoload these alias commands, create a .sample_profile file and add the alias on the file.
after that add this text in .bashrc file
.bashrc
if [ -f ~/.sample_profile ]; then
. ~/.sample_profile
fi

Environment variable PATH on linux

Hi i am currently trying to set keywords for my terminal to launch some software without having to type the whole path.
For exemple:
firefox
#instead of
/home/debian/firefox/firefox
I always do this kind of thing on windows by setting path in the environment variable manager.
After i read this post PATH environment variable in linux , i added this line to the etc/environment file:
export firefox=/home/debian/firefox/firefox
#I also tried this:
export PATH=$PATH:/home/debian/firefox
It doesn't work, can someone explains me how to do this?
I would setup a new alias in my .bashrc or .profile, which should be located under your home directory. Add the following to the end of the file:
alias firefox="/home/debian/firefox/firefox"
Save the file and reload it using:
source ~/.bashrc
Since you added the alias to your .bashrc this alias will be created everytime you open a new instance of a shell.
You could use nohup to keep the command running after the shell session ends:
alias firefox="nohup /home/debian/firefox/firefox &"
Notice the trailing & character, which will run the command in the background so you can keep using your terminal.
You can also make an alias in your .bashrc file.
$ vim ~/.bashrc
It will open your .bashrc in read mode. Get in write mode by pressing i. You can create alias at anywhere in the file or below already created alias list.
alias firefox='/home/debian/firefox/firefox'
press Esc and then :wq
This will create your alias, save and exit the file. Now you only have to compile .bashrc by this
$ source ~/.bashrc
After this you can only have to use firefox instead of long /home/debian/firefox/firefox
Adding /home/debian/firefox to your PATH should have done it.
Did you start a new shell after making that change? Otherwise the new PATH would not have exported yet. Alternatively, you can just run export PATH=$PATH:/home/debian/firefox directly to update it for your current session.

Resources