Why Some Linux Commands Need ./ and How to Make Your Programs Behave Like Built‑ins
This article explains why built‑in commands can be run directly while user programs often require a ./ prefix, describes the shell's lookup process through aliases, built‑ins, and the PATH variable, and shows how to make custom executables behave like native commands.
Introduction
In Linux, built‑in commands are executed by simply typing their name (e.g., mv a b), whereas a program you compiled must be invoked with ./program. The article explores the reasons behind this difference.
How the Shell Runs a Program
When you type a command, the shell actually runs an executable file. If the command name does not contain a path, the shell searches for it in several places.
Alias Lookup
The alias command defines shortcuts. Running alias shows all defined aliases. If an alias matches the command name, the alias is expanded and executed.
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'Built‑in Command Lookup
Shells contain built‑in commands that are executed without searching the filesystem. You can list them with help. The type command shows a command’s type; if it reports “builtin”, the shell runs it directly.
$ type echo
echo is a shell builtinPATH Lookup
If the command is not an alias or built‑in, the shell searches the directories listed in the PATH environment variable. For example:
$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/gamesThe first matching executable found is run. You can verify a command’s location with whereis:
$ whereis ls
ls: /bin/ls /usr/share/man/man1/ls.1.gzRunning Programs
If you move an executable out of a directory in PATH, the shell can no longer find it:
$ mv /bin/ls /tmp/ls_bak
$ ls
The program 'ls' is currently not installed. You can install it by typing:
apt install coreutilsTo run a program that is not in PATH, you must specify its path, either relative ( ./program) or absolute ( /tmp/program).
Specifying an Interpreter
When the shell encounters a text file without an explicit interpreter line, it treats it as a shell script. Adding a shebang (e.g., #!/bin/bash) tells the shell which interpreter to use. Changing the shebang to #!/usr/bin/python makes the same file run as a Python script, which will fail if the content is not valid Python.
#!/bin/bash
echo -e "hello world"
# Change to Python interpreter
#!/usr/bin/python
echo -e "hello world" # → SyntaxErrorMaking Your Program Behave Like a Built‑in
Three common methods:
Place the executable in a directory that is already in PATH (e.g., /bin).
Extend PATH to include the directory containing your program: PATH=$PATH:./ # only for the current shell Create an alias that points to the program’s full path:
alias hello="/temp/hello"Command Search Order
Use type -a command to see the full lookup order. For example, type -a printf may show an alias, a built‑in, a system binary, and a local file. Removing the alias with unalias printf makes the shell fall back to the next entry.
$ type -a printf
printf is aliased to `printf "hello
"`
printf is a shell builtin
printf is /usr/bin/printf
printf is ./printfConclusion
The shell resolves a command by checking aliases, built‑ins, and then the directories in PATH. Text scripts need a shebang to specify an interpreter; otherwise they are treated as shell scripts. By adjusting PATH or defining aliases, you can make your own programs callable just like native commands.
Signed-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.
