QingzhiHu / Practise

Experiment
0 stars 0 forks source link

How does one make aliases and environment variables permanent on a Mac? #1

Open QingzhiHu opened 7 years ago

QingzhiHu commented 7 years ago

How does one make aliases and environment variables permanent on a Mac?

Well, I am very interested in the topic, seeing there are problems occured when I updated my systems. Let’s us firstly introduce how to make environment variables permanent on a Mac and the similar principles would apply to make aliases permanent.

Background:

1) Mac usually uses ‘bash’ as default shell.

2) The loading order of the environment variables on mac:/etc/profile /etc/paths ~./bash_profile ~/.bash_login ~/.profile ~/.bashrc

/etc/profile and /etc/paths are of system level and once your os system sets up, it will load automatically. The other environment variables above are all of uses level. If ~/.bash_profile exists, the system won’t read ~/.bash_login ~/.profile. If ~/.bash_profile doesn’t exist, then the system will read ~/.bash_login. Only if ~/.bash_login doesn’t exists, the system will read ~/.profile. However, usually the ~./bashrc will always be loaded when your open your bash shell.

The grammar for setting the path:

usually the format is

 export
 PATH=$PATH:\<PATH 1\>:\<PATH 2\>:\<PATH 3\>:------:\<PATH N\>

1. Global Setting

The setting of these files are global, which means that you need root permissions when you change them.

1. /etc/paths (this file is suggested to change if you want to change variables under global settings).
2. /etc/profile (not suggested to change this file)
3. /etc/bashrc (this file is often used to add environment variables of system level)
4. Create a new file
$ sudo touch /etc/paths.d/MySQL

open the file and edit it using vim

$ sudo vim /etc/paths.d/mysql

type ‘i’ to edit the file you created and type ‘shift+;/:’ then enter ‘wq’ to save and quit

(save this file: /use/local/mysql/bin)

close the terminal and open a new terminal shell, then you can use ‘mysql’ command you just created.

In this way, you don’t need to put all the variables into paths file, which makes it easier to manage sometimes.

2. Setting for Single User

Edit these files: ~/.bash_profile ~/.bashrc

Use the grammar mentioned to set the path.

If you want your changes to be effective immediately (or you need to open a new terminal window), then type this command:

$ source ‘filedirectory’

for example:

$ source ~/.bash\_profile

Potential problem:

If you open a new shell and find the file unchanged (your operation is invalid) and it warns you

zsh: command not found: homestead

The problem may be due to the fact that you installed ‘zsh’ on your computer. If zsh is installed, your ~/.bash_profile won’t be executed. There are two solutions:

The first solution:

$ vim ~/.zshrc 

edit ~/.zshrc and add your environment variables to this file

The second solution:

$ vim ~/.zshrc 

add source ~/.bash_profile into this file: ~/.zshrc

Concrete Explainations with Examples

1. Types of variables

1.1 To make variables permanent, you need to change configuration files;

1.2 To make variables temporary, you can use ‘export’ command [export variable name = variable value] in the shell, and the variables you changed will be invalid after closing the shell. Next time you open a new shell, you need to redefine the variables.

2. Two methods to set variables and make them permanent

2.1 Add variables in the files under this route: /etc/profile All shells will use this profile. In other words, it will function for all users under this Linux system, and it is permanent.

e.g. add CLASSPATH variable

$ vi /etc/profile

e.g.

export CLASSPATH=./JAVA\_HOME/lib; $JAVA\_HOME/jre/lib

If you want the operation you added to take effect immediately, you need to run:

$ source /etc/profile

2.2 Add variables in the .bash_profile It will work for the current user. However, there is a chance if you mac system updated, you may lose the variables you set. Then you need to follow the instruction in 2.1.

Example 1: Change PATH variable

open the terminal and insert this command to check the current value of the PATH variable.

$ echo $PATH
Type this command to edit the .bash_profile Do remember to use ‘sudo’ in case you don’t have the rights to save this file.
$ sudo vi ~/.bash\_profile

press ‘i’ and insert this line at the end of the file:

export PATH=$PATH:/carrot/notexist

then press ‘shift+:/;’ then type ‘wq’ to save the file and quit.

$ source ~/.bash\_profile 

Otherwise it will only execute next time you open a shell.

You can check the operation you did by using this command:

$ echo $PATH

Example2: Add JAVA_HOME variable to the file .bash_profile.

$ vi ~/.bash\_profile

Then you add

JAVA\_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0\_60.jdk/Contents/Home

If you want the operation you added to take effect immediately, you need to run:

$ source ~/.bash\_profile 

Otherwise it will only execute next time you open a shell.

You can check the operation you did by using this command:

$ echo $JAVA\_HOME

3. Make alias permanent

Every time you submitted your code the command contains a series of ‘messy-parameters’ (at least it seems to me, maybe an inappropriate metaphor) which you don’t want to remember, and ‘alias’ could improve the situation:

alias aCommandAlias = ‘aCommand messy-parameters’

e.g.

alias gpush=‘Git push origin HEAD:refs/for/master’

Therefore, you only need to type push at your shell rather than type something with the long messy-parameters.

The problem is that the command won’t become effective after you reboot/restart. The solution is to edit ~/.bashrc file and insert alias command every line.

Here are the steps to follow:

$ cd
$ vi .bash\_profile

if you don’t want to use ‘cd’, you may type ‘vi ~/.bash_profile’

press ‘i’ on your keyboard to enter the edit pattern so that you can change the text.

e.g.

alias cl=‘clear’
alias pull=‘git pull’
alis cdroot=‘cd /‘
alias gst=‘git status’

save the file and run

$source ~/.bashrc

now it is done. If it doesn’t work sometimes, it may be 1) ~/.bash_profile doesn’t exist 2) .bashrc doesn’t execute

Then you need to open (if no such file, you need to make a new file) ~/.bash_profile

Then insert this command:

$ source ~/.bashrc

That’s all.

(By the way, you can also use this command to change the content of the alias : vim /etc/profile) If you don’t have vim editor installed, you can download it online in the official website.

4. You may want to check your variables after changing them.

4.1 ‘echo’ for checking a single environment variable

e.g.

echo $PATH

4.2 ‘env’ for checking all environment variables

e.g.

env

4.3 ‘set’ for checking local defined environment variables.

‘unset’ for deleting certain environment variables

e.g.

$ VAR1=‘Hello World’
$ echo $VAR1
$ unset VAR1
$ echo $VAR1

Check here for more information: https://www.tecmint.com/set-unset-environment-variables-in-linux/

5. Some frequently used environment viariables

PATH, HOME, HISTSIZE, LOGNAME, HOSTNAME, SHELL, LANGUGE, MAIL, PS1, etc

Check here for more information: https://www.cyberciti.biz/faq/linux-list-all-environment-variables-env-command/

In the case of the student's laptop I tried with, there was no ~/.bashrc yet. Is that normal on a Mac?

Open the finder and press 'command+shift+g' then go to the folder /etc, check if there is a .bashrc file. I don't think it is normal if you cannot find bashrc file there. The problem may be that it doesn't automatically load ~/.bashrc file when you open a new shell.

Something remarkable is that the new terminal window you opened on Mac is a login shell (that is interavtive). Sourced files: ~/.bash_profile for bash. For non-login shell (still interactive), sourced files: /etc/bashrc and ~/.bashrc for bash. Therefore, bash won't read ~/.bashrc automatically until you add this line into the ~/.bash_profile

$ source ~/.bashrc

In addition, another solution if you want to read ~/.bashrc after opening your teminal window, you can add this line into the ~/.bash_profile: [[ -s ~/.bashrc ]] && source ~/.bash (it means when .bashrc isn't empty, source it.)

Also for aliases, as they are not inherited, so, if you only define them in .bash_profile, they won't be defined in non-login shells. For systems using bash, if you want aliases to work in both login and non-login shells, put them in .bashrc and source .bashrc in your .bash_profile. Add this line into your .bash_profile [ -r ~/.bashrc ] && source ~/.bashrc

Under 'Global Setting" and "Conclusion":

I think I made a mistake by reffering to different questions. The methods introduced under both global setting and users' setting would work, but it is not suggested to change the system's behaviour. A common approach is to change settings in your home directory and it would work for most cases even if you update your system. However, some users who use system 10.11 reported their myeclipse or eclipse (GUI applications) cannot recognize/apply environment variables. I found a similar problem here: https://serverfault.com/questions/16355/how-to-set-global-path-on-os-x To put it briefly, the solution is to edit /etc/paths or /etc/path.d or /etc/launchd.conf A more tricky problem occured when I update my system to macOS Sierra 10.12.3. After changing the content of /etc/profile and add some environment variables with command sudo vi /etc/profile, myeclipse cannot get the variables I set even if I tried to change these files: .plist ~/.launchd.conf ~/.MacOSX/environment.plist As mentioned above, launchd.conf would work for 10.11, but not 10.12. I don't know if there is a solution and I googled a bit. Some people say they can make it run if they set the variable by using launchctl setenv. I am not very familiar with this, it is something like: sudo vi /etc/profile enter your password, press i, type the following into the second line: XX=whatever:you:want launchctl setenv XX $XX XX is the environment variable you want to set, =variable value. save it and reboot. I don't know if there is a better way to solve this problem and hopefully someone here may know.

Under "Global Settings", "Creating a new file"

"mysql" and "MySQL" is the same file on my pc. I don't remember whether I have changed some settings before, but I think for files' name, it is insensitive regarding upper/lower case. For command, it should be sensitive. Again, I just found an article which may explain this question better than I do: http://apple.stackexchange.com/a/22304

P.S. I am also looking for participants/students in the C++ course to team up with so that we can work together. If you feel interested, please email me! q.hu.2@student.rug.nl You can get to know me better here: https://www.linkedin.com/in/qingzhi-hu-823ab0100/

kumkee commented 6 years ago

@QingzhiHu 我看到你有很努力学习呢。 假以时日,你对自己进度满意又想做别的实际的项目的话赚点外快,我们欢迎你来帮忙,全职兼职都可以。 我们在策划成立公司,到时就会有正式工资发给你的。