Unlock the Power of Shell: Environment Variables, PATH, and File Search
Learn how to create and manage shell variables, differentiate between local and environment scopes, manipulate the PATH variable, and efficiently locate files using whereis, locate, which, and find commands, complete with practical examples and step‑by‑step instructions for Unix‑like systems.
1. Environment Variables
1.1 Variables
Shell variables have different types, can participate in arithmetic, and are limited by scope.
Variable scope is the range where a variable is valid (e.g., within a function, a source file, or globally). Only one variable with the same name can exist in that scope, and it becomes invalid once the scope is left.
# 使用 declare 命令创建一个变量名为 tmp 的变量
declare tmp
tmp=hello
echo $tmp1.2 Environment Variables
Environment variables have a broader scope than user‑defined variables; they affect the current shell and its child processes. In Unix‑like systems each process inherits most of its parent’s environment.
Typical environment variables fall into three categories:
Current shell private user‑defined variable (e.g., temp), valid only in the current shell.
Shell built‑in variables.
Variables exported from user‑defined variables to the environment.
Key commands for inspecting and exporting variables:
set : shows all shell variables, including built‑ins, user‑defined, and exported variables.
env : displays environment variables related to the current user and can run a command with a modified environment.
export : lists variables exported to the environment and can export a user‑defined variable.
tmp=hello
echo $temp
# create a child shell
bash
echo temp # empty because variable is not exported
exit
export temp
bash
echo $temp1.3 Command lookup path and order
The shell locates commands using the PATH environment variable.
# 查看 PATH 环境变量的内容
echo $PATHExample of creating a simple shell script, making it executable, and running it:
# create script file
vim hello_shell.sh
# script content
#!/bin/zsh
for ((i=0; i<10; i++)); do
echo "hello shell"
done
exit 0 # make it executable
chmod 755 hello_shell.sh
# run the script
./hello_shell.sh1.4 Adding custom path to PATH
PATH=$PATH:/home/testThis change only affects the current shell; to make it persistent add the line to the user’s startup file ( .bashrc for Bash, .zshrc for Zsh).
echo "PATH=$PATH:/home/test" >> .bashrc1.5 Modifying and deleting variables
Shell parameter expansion provides several ways to remove or replace substrings:
path=$PATH
echo $path
# remove shortest match from the end
path=${path%/home/test}
# or using wildcard for any characters
path=${path%*/test}1.6 Making environment variable take effect immediately
source .bashrc2. File Search
Common commands for locating files are whereis, locate, which, and find.
2.1 whereis (quick, limited)
whereis whoSearches for binaries (-b), man pages (-m), and source files (-s). For more comprehensive results use locate.
2.2 locate (fast, uses database)
Searches the database /var/lib/mlocate/mlocate.db, which is updated daily by updatedb.
# search etc directory recursively
locate /etc/sh
# find all jpg files under /usr/share
locate /usr/share/*.jpgOptions: -c to count matches, -i for case‑insensitive search.
2.3 which (small, precise)
which man2.4 find (powerful and flexible)
Find can filter by name, type, and file attributes such as timestamps.
find /etc/ -name interfacesNote: the first argument is the search path; basic syntax is find [path] [option] [action] .
Time‑related options:
-atime : last access time.
-ctime : creation time.
-mtime : last modification time.
Examples with -mtime: -mtime n: files modified within the day n days ago. -mtime +n: files modified more than n days ago (excluding day n). -mtime -n: files modified within the last n days (including today). -newer file: files newer than the specified file.
# list files in home modified today
find ~ -mtime 0
# list files newer than the Code directory
find ~ -newer /home/shiyanlou/CodeSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
