Tanuja Tiwari
Tanuja Tiwari

Reputation: 61

The touch () command in Linux is asking for an input. What are we supposed to type in there?

I was playing around with the touch command and as I entered the touch () command, I expected either an error or a new empty file named (). Instead, I got this

touch ()
> 

and it expects me to enter something.
What is this touch () command doing? What is it for?

Upvotes: 1

Views: 305

Answers (2)

agc
agc

Reputation: 8406

This question is a bit of an illusion, based on a confusion between types of commands -- some commands are keywords built in to the shell, (like for and if), some are aliases, (like ls and ll and l on most distros), some are functions, but most are executables.

While there is a touch command, (in Ubuntu it activates the executable /usr/bin/touch), the code touch () never goes near it.

As iBug's answer notes, the () tells the shell that a function is being defined, but since there's no code, the shell prompts the user to enter some.

This isn't specific to the touch command however. The shell will do the same with any command, whether real:

 bash ()
 less ()

Or made up:

foobarbaz()

There are exceptions. For example, if the command is an alias, like ls, this happens:

ls ()
bash: syntax error near unexpected token `('

Upvotes: 1

iBug
iBug

Reputation: 37287

With touch (), your shell is expecting you to define a function, something like this:

touch () {
  echo "Hello"
}

(don't do that)

Since () are shell metacharacters, if you want to create files with such names, escape them or put them in quotes:

touch \(\)
touch '()'
touch "()"

Upvotes: 3

Related Questions