Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use a variable that contains a space?

Tags:

bash

I have the following files:

~/tmp/testbash$ l
file 1.test  move.sh*

where move.sh is:

#!/bin/bash
#-x

FILENAME='file\ .test'
echo $FILENAME
echo joo
mv $FILENAME test.test

When I run ./move.sh, I get this output and error:

file\ .test
joo
mv: target `test.test' is not a directory

The problem is that it executes the command as:

mv file .test test.test

and not as:

mv file\ .test test.test

How can I fix this?

like image 440
1408786user Avatar asked Jun 01 '12 08:06

1408786user


People also ask

Can a variable contain a space?

Variable names cannot contain spaces.

How do you use variable names in space?

run; The options validvarname=any; tells SAS to allow you to have variable name begin with or contain spaces, special characters or numbers. Additionally, we need to put variable name having spaces in quotes followed by the letter n.

How do you declare a variable in space?

You define a variable name that contains spaces or special characters by writing the name between single quotes (') followed by the character n, for example ' My new Variable'n . Make sure that you use the global SAS-option validvarname=any , before you run your code.

Can we give space in between variable name?

SQL does allow you to "quote" variable names with either [name with space] or with backticks `name with space` . Most other languages do not allow spaces in variable names, because any whitedspace is considered a separator for different lexical unit [different name/word/variable].


1 Answers

If the variable contains embedded spaces, then bracket the variable in double quotes (").

FILENAME='file .test'
mv "$FILENAME" test.test
like image 110
Ignacio Vazquez-Abrams Avatar answered Oct 13 '22 01:10

Ignacio Vazquez-Abrams