InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
Bash
Handling Parameters with Blanks in bash
Bash

Handling Parameters with Blanks in bash

InfinityCoder February 14, 2017

You wrote a script that took a filename as a parameter and it seemed to work, but then one time your script failed.

The filename, it turns out, had an embedded blank.

You’ll need to be careful to quote any shell parameters that might contain filenames.
When referring to a variable, put the variable reference inside double quotes.

Thanks a lot, Apple! Trying to be user friendly, they popularized the concept of space characters as valid characters in filenames, so users could name their files with names like My Report and Our Dept Data instead of the ugly and unreadable MyReport and Our_Dept_Data.

(How could anyone possibly understand what those old-fashioned names meant?)

Well, that makes life tough for the shell, because the space is the fundamental separator between words, and so filenames were always kept to a single word.

Not so anymore.
So how do we handle this?
Where a shell script once had simply ls -l $1, it is better to write ls -l “$1” with quotes around the parameter.

Otherwise, if the parameter has an embedded blank, it will be parsed into separate words, and only part of the name will be in $1.

Let’s show you how this doesn’t work:

1
2
3
4
5
6
7
$ cat simpls.sh
# simple shell script
ls -l ${1}
$
$ ./simple.sh Oh the Waste
ls: Oh: No such file or directory
$

When we don’t put any quotes around the filename as we invoke the script, then bash sees three arguments and substitutes the first argument (Oh) for $1.

The ls command runs with Oh as its only argument and can’t find that file.
So now let’s put quotes around the filename when we invoke the script:

1
2
3
4
5
$ ./simpls.sh "Oh the Waste"
ls: Oh: No such file or directory
ls: the: No such file or directory
ls: Waste: No such file or directory
$

Still not good. bash has taken the three-word filename and substituted it for $1 on the ls command line in our script. So far so good.

Since we don’t have quotes around the variable reference in our script, however, ls sees each word as a separate argument, i.e., as separate filenames.

It can’t find any of them. Let’s try a script that quotes the variable reference:

1
2
3
4
5
6
7
$ cat quoted.sh
# note the quotes
ls -l "${1}"
$
$ ./quoted.sh "Oh the Waste"
-rw-r--r-- 1 smith users 28470 2007-01-11 19:22 Oh the Waste
$

When we quoted the reference “{$1}” it was treated as a single word (a single filename), and the ls then had only one argument—the filename—and it could complete its task.

 

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Checking a tar Archive for Unique Directories in bash
You want to untar an archive, but you want to …

Checking a tar Archive for Unique Directories in bash

Parsing a CSV Data File in bash
You have a Comma Separated Values (CSV) data file that …

Parsing a CSV Data File in bash

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap