Why can Linux commands be executed without writing a PATH?

Introduction

When I was touching Linux recently, I wondered why commands such as ls, cd, pwd can be executed without writing the PATH to the executable file, which made me write this article.

table of contents

  1. Environment variables
  2. Pass through PATH

1. Environment variables

Before we talk about PATH, we must first touch on this environment variable.

Environment variables are those that the OS permanently saves the setting values and so on so that they can be set and referenced by users and executed programs. It is used to record setting values that differ for each user and computer, which are required when executing a program. (IT Glossary e-Words)

Simply put, it feels like you can set a value in a variable for each user, or you can set it for the entire computer.

So how do you set environment variables?


$ NAME=Tanaka
$ export NAME
    # or
$ export NAME=Tanaka

In this way you can set environment variables in Linux. However, environment variables cannot be saved permanently in this state. So now you have to set the environment variable in ~ / .bash_profile.

#.bash_profile

NAME='Tanaka'
export NAME

This allows you to set environment variables permanently. By setting soruce ~ / .bash_profile, you can log out and reflect without logging in.

2. Pass through PATH

By the way, the main subject of this article, ls, pwd, cd, etc., can be executed without going through PATH, it means that PATH is set in the environment variable.


$ echo $PATH
#The status set in the current variable PATH is displayed.
#Example) /usr/bin:/usr/local/sbin etc.

By setting to this PATH, ls, cd, pwd, etc. can be used without going through the PATH.

# ~/bash_profile
PATH="$PATH:/usr/bin"
export PATH

Summary

The reason why ls, cd, pwd, yum, etc. that I used casually can be used without passing through the PATH can be used by passing through the environment variable PTAH. The process of passing the PATH when downloading some executable file Something is so.

Next time, I'd like to write an article about the ~ / .bash_profile and source commands that I didn't touch on in detail.

Reference material

https://ja.wikipedia.org/wiki/%E7%92%B0%E5%A2%83%E5%A4%89%E6%95%B0 https://qiita.com/Naggi-Goishi/items/2c49ea50602ea80bf015

Recommended Posts

Why can Linux commands be executed without writing a PATH?
I made a LINE stamp where you can study Linux commands
Can I be a data scientist?
[Linux] Command to get a list of commands executed in the past
A class for PYTHON that can be operated without being aware of LDAP
Since python is read as "Pichon", it can be executed with "Pichon" (it is a story)