Questions tagged [shell-builtin]

A shell builtin is a command called from a shell, that is executed directly in the shell itself.

Filter by
Sorted by
Tagged with
127
votes
7answers
19k views

Why is cd not a program?

I've always wondered why cd isn't a program, but never managed to find the answer. Anyone know why this is the case?
72
votes
4answers
21k views

What is the difference between a builtin command and one that is not?

Is there any intrinsic difference between a builtin command and another command which can nominally do the same thing? eg. Do builtins get "special" treatement? ... is there less overhead running ...
48
votes
2answers
11k views

Can I get individual man pages for the bash builtin commands? [duplicate]

Is there anywhere you can download a manpage for every builtin commands? I know you can just use help or man bash and search to find info about it, but I want them separated, so I can just do man ...
34
votes
6answers
11k views

Why is echo a shell built in command?

$ which echo echo: shell built-in command. $ which ls /bin/ls $ which cat /bin/cat Why is echo not an independent utility like ls, ps, cat etc? Why is it shell specific? Any good reasons?
42
votes
7answers
10k views

What purpose does the colon builtin serve?

I've hacked on a lot of shell scripts, and sometimes the simplest things baffle me. Today I ran across a script that made extensive use of the : (colon) bash builtin. The documenation seems simple ...
27
votes
5answers
7k views

Make bash use external `time` command rather than shell built-in

How can I make bash use time binary (/usr/bin/time) by default instead of the shell keyword? which time returns /usr/bin/time type time returns time is a shell keyword Running time is obviously ...
62
votes
4answers
8k views

Why is `[` a shell builtin and `[[` a shell keyword?

As far as I know, [[ is an enhanced version of [, but I am confused when I see [[ as a keyword and [ being shown as a builtin. [root@server ~]# type [ [ is a shell builtin [root@server ~]# type [[ [[ ...
71
votes
4answers
82k views

What's the difference between eval and exec?

eval and exec are both built in commands of bash(1) that execute commands. I also see exec has a few options but is that the only difference? What happens to their context?
13
votes
4answers
5k views

How to invoke a shell built-in explicitly?

I want to customize the functionality of cd command as per my needs. I defined the following function - function cd () { cd "$@" && pushd "$@"; } The intent of this function is to ...
5
votes
3answers
705 views

What is “declare” in Bash?

After reading ilkkachu's answer to this question I learned on the existence of the declare (with argument -n) shell built in. help declare brings: Set variable values and attributes. Declare ...
13
votes
3answers
601 views

Slightly confused about whether printf in the yash shell is a built-in command or not

The yash shell has a printf built-in, according to its manual. However, this is what I see in a yash shell with default configuration: kk@eeyore ~ $ command -v printf /usr/bin/printf kk@eeyore ~ $ ...
-4
votes
4answers
188 views

Is there a bash builtin command which can show the environment variables of the current shell?

env and printenv are both external commands i.e. executable files, not bash builtins. The problem with them is that some environment variables (such as _, and I also wonder if there are more) which ...
12
votes
4answers
1k views

Use case / practical example for Bash' builtin exec

Consider this from the documentation of Bash' builtin exec: exec replaces the shell without creating a new process Please provide a use case / practical example. I don’t understand how this makes ...
17
votes
6answers
2k views

Use system command instead of Bash builtin without specifying the full path

I use Bash as my interactive shell and I was wondering if there was an easy way to get Bash to run a system command instead of a shell builtin command in the case where they both share the same name. ...
10
votes
2answers
3k views

Order of executables started in bash

If I execute the test command in bash, test(evaluates conditional expression) built-in utility is started: $ type test test is a shell builtin $ type -a test test is a shell builtin test is /usr/...
7
votes
1answer
2k views

How to read the man page for a bash builtin? [duplicate]

So, I needed to figure out how "exec" works. However, doing man exec brings me to a 99% useless man page describing what bash builtins are. It of course also does this with many other man pages such ...
5
votes
5answers
4k views

missing man pages of some commands

Typing man alias gives me No manual entry for alias The same thing goes for export and eval. At first I thought it only happens to shell built-in commands but man echo gives me the man page. ...
3
votes
3answers
3k views

What exactly happens when a built-in command is run in a shell?

I know that external commands are run in the shell by creating a separate process, but what exactly happens when a built-in command is run in a shell? Are they executed as a function, or does the ...
35
votes
1answer
27k views

Understand `compgen` builtin command

From help compgen: $ help compgen compgen: compgen [-abcdefgjksuv] [-o option] [-A action] [-G globpat] [-W wordlist] [-F function] [-C command] [-X filterpat] [-P prefix] [-S suffix] [word] ...
44
votes
8answers
4k views

Why are POSIX mandatory utilities not built into the shell?

The purpose of this question is to answer a curiosity, not to solve a particular computing problem. The question is: Why are POSIX mandatory utilities not commonly built into shell implementations? ...
18
votes
2answers
1k views

Why does POSIX require certain shell built-ins to have an external implementation?

From this question about whether printf is a built-in for yash, comes this answer that quotes the POSIX standard. The answer points out that the POSIX search sequence is to find an external ...
11
votes
4answers
438 views

When are the built-in commands loaded to memory

Let's say if I type in cd in my shell. Is cd loaded from the memory at that moment? My intuition is that these built-in commands are pre-loaded to the system memory after the kernel has been loaded, ...
12
votes
1answer
3k views

Using the built-in “time” command in bash rather than the external command

Working with the time command, I came across a situation where I should use the built-in time rather than the external GNU time command /usr/bin/time. So, how can I do this? I saw somewhere that ...
6
votes
2answers
2k views

Capture multi-line output of a bash builtin

Normally, bash is able to assign multi-line output of a command: L=`ls` This works from both interactive shell and scripts. But it appears the output of a builtin can't be captured in a variable: L=...
10
votes
2answers
16k views

Count number of elements in bash array, where the name of the array is dynamic (i.e. stored in a variable)

Brief statement of the question: Is there built-in bash method to count number of elements in bash array, where the name of the array is dynamic (i.e. stored in a variable), without resorting to ...
4
votes
1answer
337 views

Why are parameters to Bash's builtin optional?

Running simply builtin prints nothing and returns exit code 0. This is in accordance with help builtin, which shows all parameters as optional. But why isn't this no-op an error? Is there a use case ...
2
votes
2answers
416 views

How to distinguish between builtin and external util? (e.g. echo)

Some commands are provided as both builtins and external utilities. Take echo for example. On my machine (macOS) running Bash 3.2, $ type echo echo is a shell builtin Running man bash | less --...
1
vote
1answer
191 views

How to capture stderr of a bash keyword (e.g. time)?

I want to time reading bash history and put the result into a variable. Unfortunately, time (which is a shell keyword) prints the result to stderr, and apparently redirecting stderr from shell ...
1
vote
2answers
921 views

How to specifically run a shell builtin command

Consider a situation that I run these commands in my current shell or I put them inside .bashrc: alias source='echo hi' alias .='echo hi' alias unalias='echo hi' Or function source(){ echo hi; }, ...
0
votes
2answers
185 views

When a shell runs a builtin, and a signal is generated by keyboard, what will handle the signal?

Please correct me if I am wrong: When a bash shell runs an external executable program, the bash shell will create a child process to run the program in foreground. If there is any key-generated ...
-3
votes
2answers
173 views

rm -rf ./* deletes content of current directory only partially [duplicate]

rm -rf ./* deleted all content of current directory only partially. Input: MN> cd ~/mydir MN> rm -rf ./* MN> ll Output: -rw-r--r-- 1 MN MN 1025 Aug 1 15:50 .file1 -rw-r--r-- 1 MN MN ...