0

On my exam I had this question:

Indicate which commands are NOT built-in.

a. info

b. help

c. echo

d. man

e. ls

f. cd

g. pwd

h. mv

And i said that the not built in are info, man, ls and mv, but I didn't get the full points, I seem to have missed some. To check if a function is built in I used "type". Can someone tell me what I missed and what is the corect method to check this?

Red Smurf
  • 7
  • 1
  • I think you were entirely correct. Using Bash (the default shell on Linux Mint), you can force the use of a builtin by running `builtin `, and using that technique with your list of commands will yield `not a shell builtin` for `info`, `man`, `ls`, and `mv`. – larsks Jun 03 '23 at 11:59
  • 1
    Was the question about `bash` explicitly or about `sh`? On some systems `sh` is really `bash`, but on others it is `dash`. `help` is not a built-in for `dash`. See [Difference between sh and Bash](https://stackoverflow.com/q/5725296/4154375). – pjh Jun 03 '23 at 16:54
  • Thanks. It turns out that on linux mint /bin/sh is [tag:dash-shell]. I have updated my answer – Andrey Bienkowski Jun 04 '23 at 07:24

3 Answers3

1

I would have given the same answer: info, man, ls and mv are not built-in, the rest are built-in. I do not think this is likely to be different on different versions of bash or different Linux distributions (assuming you are using bash and not another shell).

$ echo "$BASH_VERSION"
4.4.23(1)-release
$ type info
info is /usr/bin/info
$ type help
help is a shell builtin
$ type echo
echo is a shell builtin
$ type man
man is /usr/bin/man
$ type ls
ls is /usr/bin/ls
$ type cd
cd is a shell builtin
$ type pwd
pwd is a shell builtin
$ type mv
mv is /usr/bin/mv

Try it online!

I see several possibilities:

  • You made an error when filling out the form (e.g. ticked the wrong box)
  • Your teacher made an error when checking your answers
  • The question was about a shell other than bash. Maybe echo is not built-in in some shells or maybe ls is a built-in
  • The question is somewhat poorly worded and is asking which commands are available as external executables (even if they are shadowed by built-ins). In my experiments this adds echo, pwd and surprisingly cd
$ type -P help
$ type -P echo
/usr/bin/echo
$ type -P cd
/usr/bin/cd
$ type -P pwd
/usr/bin/pwd

Try it online!

As suggested by @pjh I have checked what is the default shell on linux mint. Turns out it is rather than Note: at the time of writing the question is tagged

$ file /bin/sh
/bin/sh: symbolic link to dash

It turns out that dash unhelpful (help: not found)

$ type info
info is /usr/bin/info
$ type help
help: not found
$ type echo
echo is a shell builtin
$ type man
man is /usr/bin/man
$ type ls
ls is /usr/bin/ls
$ type cd
cd is a shell builtin
$ type pwd
pwd is a shell builtin
$ type mv
mv is /usr/bin/mv

Try it online!

1

Arguably, the question is ambiguous. Is pwd builtin? Yes... also no, because /bin/pwd as a non-builtin exists. Same for echo, it's builtin, but also /bin/echo which is not builtin exists. So when they say is echo NOT builtin... well when it comes to /bin/echo is not builtin, so... I would have asked instead, which of these commands do not have a builtin version.

Sometimes you will get the non-builtin without giving it much thought. For example exec echo Hello World

executes one command ... "echo" and it's not the builtin.

xpusostomos
  • 1,309
  • 11
  • 15
1

Since you tagged the question with bash, I assume that you want to know a bash solution.

The solution which immediately springs to mind is to use type, for instance

type cd

which should answer: cd is a shell builtin

However, there is a caveat. If someone would have overridden the command, for instance by

alias cd='echo x'

you would now get as answer: cd is aliased to 'echo x'

So, do get a more reliable solution, you would do a

type -a cd

and this would then display

cd is aliased to 'echo x'

cd is a shell builtin

cd is /usr/bin/cd

However, this is still not safe. What if someone would have redefined the type command, i.e.

alias type='echo y'

To catch this case as well, the final recommended solution would be to use

'type' -a cd

The quotes would tell bash to use the builtin type command.

user1934428
  • 19,864
  • 7
  • 42
  • 87