Linux Labs

Bash: text processing and scripts / Stage 5 of 9

Arguments, quotes and file checks

Not running Starting usually ~20 s
Leave the lesson

Step 1 of 7 · on a machine · 4 min

A file with a space in its name

What you will learn

  • Pass names with spaces to a script and keep them whole inside with "$1" and "$@".
  • Test a path with a condition before working with it — its type, read permission and size.
  • Answer with an exit status and a message on stderr when the script is called the wrong way.

The report.sh script counts the lines in the files you pass to it. It copes with notes.txt. Now you need the line count of my file.txt. The name has a space, so you put it in quotes, as in the lesson “Help and the shell”.

Exercise · look

Pass the script a name with a space

You are in ~/args-lab. Count lines with the report.sh script: first in notes.txt, then in “my file.txt” — the name in quotes.

First, predict

“my file.txt” has three lines, and the name is in quotes. What will ./report.sh "my file.txt" print?

Now start the exercise, run the command and compare your prediction with the output.

Sign in and start The machine is available after signing in
Check yourself 1 question

Why did the name fall apart into my and file.txt even though you put it in quotes?

  • ls shows one file, my file.txt. There are no files called my and file.txt — that is exactly what wc reported.

  • Quotes change nothing in the name; they keep it as one word. ls shows the file my file.txt with its space.

  • wc -l "my file.txt" in the terminal prints 3 my file.txt: given the whole name, wc opened the file.

The name reached report.sh as one argument and fell apart again inside the script.

Ubuntu 24.04
Loading…