I ran into a code while installing Ruby on Ubuntu at this site.
#Add path
$ echo 'export PATH="
#Addition of rbenv initialization script $ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
What the hell is this ...
At first I thought, but as I researched it, I understood it to some extent, so I will summarize it in an article so that I will not forget it.
If you make a mistake, please let me know in the comments.
# About echo
First, let's talk about the command ʻecho`. See [here](https://eng-entrance.com/linux-command-echo) for a detailed description of ʻecho`. There are three ways to use ʻecho` used in the first command I wrote.
>```sh
#Add the character string to the file.
$echo string>>file name
># 「$Display the string "variable".
$ echo '$variable'
># 「$Display the contents of a variable called "variable".
$ echo "$variable"
Now go back to the first code.
$ echo 'export PATH="
Both commands have the form ʻecho string >> ~ / .bash_profile`.
In other words, I'm adding the string to a file called .bash_profile in my home directory. This .bash_profile will be explained later.
Regarding the contents of the string, `'export PATH =" $ HOME / .rbenv / bin: $ PATH "'`, export is a command that allows you to define environment variables. So this sets the environment variable `PATH` to the value` "$ HOME / .rbenv / bin: $ PATH" `. Environment variables will be explained later.
Similarly, the contents of the character string are `'eval" $ (rbenv init-) "'`, but eval is a command that concatenates the specified character string after evaluation and causes the current shell to execute it. [Have a look at this. Actually, I didn't know much about eval, so I'll try to find out a little more.
In my interpretation, this command causes the shell to execute the command `rbenv init -`.
## About .bash_profile
The .bash_profile that came out earlier, but [this article](https://neos21.hatenablog.com/entry/2017/02/12/142817) is easy to understand.
To summarize briefly, bash_profile is a file that runs only once at login.
Considering it together with the previous story
>```sh
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
Means to define $ HOME / .rbenv / bin: $ PATH
in PATH
only once at login and run rbenv init -
.
Next, let's talk about what it means to "pass through". In conclusion, "passing through" means
Change the description of the environment variable ($ PATH) and add the command search path.
Quoted from this site. Please read it because it has a very detailed description of "passing through".
Returning to the story, environment variables are variables stored in your personal computer. You can check the environment variables with the following command.
The name of the printenv environment variable
#If no argument is added after printenv, all environment variables will be displayed. printenv
If you execute `printenv`, the environment variables will be displayed in a row, and you can see the environment variable` PATH` in it.
What role this `PATH` plays is that it tells the shell (terminal) where the executable is located.
For example, in the command `printenv` that came out earlier, the shell refers to the executable file` / usr / bin / printenv`. Therefore, the following two commands are the same.
>```sh
$ /usr/bin/printenv
$ printenv
The reason why / usr / bin
can be omitted at this time is that this is described in PATH
.
When you execute printenv PATH
, it will be described as/ home / [user name] / bin: ...
. It has paths separated by: and is looking for the location of the executable in order from the first path.
Let's take a look at the first command I wrote here.
$ echo 'export PATH="
I told you that this command defines the value `$ HOME / .rbenv / bin: $ PATH` in` PATH`. Here, `$ HOME / .rbenv / bin: $ PATH` is enclosed in" ", so` $ HOME` and `$ PATH` refer to the values in the environment variables. The value of `$ HOME` is` / home / [user name] `in my case, and` PATH` is `/ home / [user name] / bin: ...` as before.
That is, the value of `$ PATH` defined by ʻexport PATH =" $ HOME / .rbenv / bin: $ PATH "` is `/ home / [username] /.rbenv/bin:/home/[username] / bin: ・ ・ ・ `. This will allow the shell to reference the executable in `/ home / [username] /. Rbenv / bin`.
# Finally
No, it was long. At first I thought it was a mess, such as passing through a path or .bash_profile, but when I looked it up, it was surprisingly interesting to understand. Now that the rails environment is in place, I would like to summarize what I learned from rails.
Recommended Posts