Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Introduction to the Terminal!
You have completed Introduction to the Terminal!
Preview
Most commands can take arguments following the command name.
The first word you enter at a command line is the name of the command. Most commands can take arguments following the command name.
- For example, if I type
cat
all by itself:cat
- It just sits there waiting for input. That's not very useful on its own.
- I'll just type Ctrl-c to interrupt the program and return to the command line.
- But if I give
cat
an argument with the name of a file, it will output the contents of that file.- Let me get a list of the files that are here:
ls
- There's a
statue.txt
file here. Let's see what it contains. I'll type the name of thecat
command, a space, and then type an argument ofstatue.txt
:cat statue.txt
- We'll see the program's output before we're returned to the command line. It's the contents of the
statue.txt
file. - These are the same contents we'd see if we opened the file in an editor. [Click "statue.txt", close.]
- Let me get a list of the files that are here:
treehouse:~/workspace$ ls
bird.txt cart.txt library mall offices statue.txt
treehouse:~/workspace$ cat statue.txt
A statue of a hunter standing over a dead bear. Creepy.
- Commands can take multiple arguments, separated by spaces.
- I can type
cat
, a space, a first argument ofstatue.txt
, another space, and a second argument ofcart.txt
:cat statue.txt cart.txt
- The cat program will take the contents of both files and concatenate them together. That's a fancy way of saying it prints the contents out for us, one file after another.
- I can type
treehouse:~/workspace$ cat statue.txt cart.txt
A statue of a hunter standing over a dead bear. Creepy.
A stand selling hot dogs and bottles of diet cola.
- Now here's another command that takes arguments: the
echo
command.echo
takes arguments and converts them into output:echo hello
- That makes
echo
a great way to understand how command line arguments work. -
echo
can take multiple arguments. It will print all of its arguments out, joined together with spaces:echo Each of these words is an argument.
- You can use any combination of characters as an argument, not just letters, so the last argument in that last command includes both the word argument and the period.
- If you need to take multiple words and convert them to a single argument, you can surround them with quotation marks.
- You can use either single quotes:
echo 'Everything between the quotes is one single argument.'
- Or double quotes:
echo "Everything between the quotes is one single argument."
- Notice that the quotation marks were consumed by the shell; they never reach the
echo
program. They're not treated as part of the resulting argument, and so they don't appear in the output. - Now with a command like
echo
, the difference between one argument and many arguments may not seem that important. - But even with
echo
, we can show you a situation where the difference matters. - Here's an echo command with many arguments, each with two spaces between them:
echo There are two spaces between each of these words.
- The spaces between arguments are also consumed by the shell, so even though there are two spaces between each argument, those spaces never reach the
echo
program.echo
just joins all its arguments together with a single space, so the double spaces are lost. - If you want to preserve the double spaces, you have to make them part of a single argument using quotes:
echo 'There are two spaces between each of these words.'
- That makes
treehouse:~/workspace$ echo hello
hello
treehouse:~/workspace$ echo Each of these words is an argument.
Each of these words is an argument.
treehouse:~/workspace$ echo 'Everything between the quotes is one single argument.'
Everything between the quotes is one single argument.
treehouse:~/workspace$ echo "Everything between the quotes is one single argument."
Everything between the quotes is one single argument.
treehouse:~/workspace$ echo There are two spaces between each of these words.
There are two spaces between each of these words.
treehouse:~/workspace$ echo 'There are two spaces between each of these words.'
There are two spaces between each of these words.
- Now let's look at a situation where the number of arguments is much more important.
- Here in the editor, I'm going to create a file with spaces in its name.
- Using spaces in a file name is normally not a good idea, for reasons we're about to see, but I'm going to do it anyway.
- Let's try looking at its contents using the
cat
command:cat file name with spaces.txt
- Unfortunately, each word in the file name gets treated as a separate argument.
-
cat
tries to print out a file namedfile
, a file namedname
, awith
file, and aspaces.txt
file. None of these files actually exist, so it reports an error for each. - To get
cat
to print outfile name with spaces.txt
, we need to surround the name with quotes, so it's treated as a single argument:cat 'file name with spaces.txt'
treehouse:~/workspace$ cat file name with spaces.txt
cat: file: No such file or directory
cat: name: No such file or directory
cat: with: No such file or directory
cat: spaces.txt: No such file or directory
treehouse:~/workspace$ cat 'file name with spaces.txt'
hi
treehouse:~/workspace$
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
The first word you enter in the command
line is the name of the command.
0:00
For example, we saw the ls, whoami,
and clear commands earlier.
0:04
Most commands can take arguments
following the command name.
0:09
For example, if I type cat all by itself,
it just sits there waiting for input.
0:12
That's not very useful on its own.
0:17
I'll just say Ctrl+C to interrupt the
program and return to the command line.
0:20
But if I give cat an argument
with the name of a file,
0:24
it will output the contents of that file.
0:27
Let me get a list of
the files that are here, ls.
0:30
There's a statue.txt file here,
let's see what it contains.
0:33
I'll type the name of the cat command,
a space, and
0:38
then type an argument of statue.txt.
0:40
We'll see the programs output before
we return to the command line.
0:44
It's the contents of the statue.txt file.
0:48
These are the same contents we'd see
if we opened the file in an editor.
0:51
Commands can take multiple
arguments separated by spaces.
0:56
I can type cat, a space,
a first argument of statue.txt,
1:00
another space, and
a second argument of cart.txt.
1:04
The cat program will take the contents of
both files and concatenate them together.
1:08
That's a fancy way of saying it
prints the contents out for us,
1:13
one file after another.
1:17
Now, here's another command
that takes arguments.
1:18
The echo command.
1:21
Echo takes arguments and
converts them into output.
1:22
That makes echo a great way to understand
how command line arguments work.
1:26
Echo can take multiple arguments.
1:31
It will print all of its arguments
out joined together with spaces.
1:33
So I can type out several arguments here.
1:39
Each one of these words
will be an argument.
1:41
And I get the exact same output back.
1:44
You can use any combination
of characters as an argument.
1:46
Not just letters.
1:49
So the last argument in that last command
includes both the word argument and
1:50
the period.
1:54
If you need to take multiple words and
1:56
convert them to a single argument, you
can surround them with quotation marks.
1:58
You can use either single quotes,
'Everything between the quotes on this line
2:02
will be a single argument'. Or
you can use double quotes.
2:06
Notice that the quotation marks
were consumed by the shell.
2:16
They never reached the echo program.
2:19
They're not treated as part of
the resulting argument, and so
2:21
they don't appear in the output.
2:24
Now, with a command like echo,
the difference between one argument and
2:26
many arguments may not
seem that important.
2:29
But even with echo, we can show you
a situation where the difference matters.
2:32
Here's an echo command
with many arguments,
2:36
each with two spaces between them.
2:38
The spaces between arguments
are also consumed by the shell.
2:41
So even though there are two
spaces between each argument,
2:44
those spaces never reach the echo program.
2:47
Echo just joins all its arguments
together with a single space so
2:49
the double spaces are lost.
2:53
If you want to preserve the double spaces,
2:55
you have to make them part of
a single argument using quotes.
2:57
Now let's look at a situation where
the number of arguments is much
3:01
more important.
3:04
Here in the editor, I'm going to
create a file with spaces in its name.
3:06
Using spaces in a file name is
normally not a good idea, for
3:10
reasons we're about to see,
but I'm going to do it anyway.
3:13
I'll create a file named file
name with spaces.txt and
3:17
I'll give it the text hi,
save it, and I'll close my editor.
3:25
Let's try looking at its
contents using the cat command.
3:31
cat file name with spaces.txt.
3:34
Unfortunately each word in the file name
gets treated as a separate argument.
3:38
Cat tries to print out a file named file,
a file named name, a with file, and
3:43
a spaces.txt file.
3:48
None of these files actually exist,
so it reports an error for each.
3:49
To get cat to print out
file name with spaces.txt,
3:54
we need to surround the name with quotes
so it's treated as a single argument.
3:57
Cat 'file name with spaces.txt'.
4:01
And now you can see the file contents
because it ran successfully.
4:05
I don't want a file name with spaces in
it hanging around in my workspace, so
4:09
I'm going to ahead and delete it.
4:13
So now you know the basics of
providing command arguments.
4:16
In the next video, we'll take a look at
a different kind of command argument.
4:19
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up