Two spaces making trouble


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Two spaces making trouble
# 1  
Old 09-20-2015
Question Two spaces making trouble

I have really big problems with two spaces. I tried to simplify my problem as much as possible. I have made a script to understand my problem:
Code:
#!/bin/bash
VAR='/path/with/two  spaces'
echo '/path/with/two  spaces'
echo $VAR

I am a beginner of scripting, but i thought until now i would guess what would be the output of the script above. But nothing i know...

Is there anybody out there who could explain me why the output is:
Code:
/path/with/two  spaces
/path/with/two spaces

If you do not see my problem, I do not know why the second line has only one space. It should be two spaces...

Thank you very much for every hint and help.
# 2  
Old 09-20-2015
Hi,
You must to protect your variable with double quotes:
Code:
$ VAR='/path/with/two  spaces'
$ echo $VAR
/path/with/two spaces
$ echo "$VAR"
/path/with/two  spaces

Regards.
# 3  
Old 09-20-2015
Expanding a little bit on what disedorgue said, ...

By quoting the string passed to echo ('word word'), the shell passes a single operand containing two spaces to echo. Without quoting $VAR, the shell expands $VAR and then splits the results into separate operands (/path/with/two and spaces) to be passed to echo. To get what you want, use:
Code:
echo "$VAR"

Note that variable expansion does not occur in single-quoted strings, but does occur between double-quoted strings. Compare the output from the above command with the output from:
Code:
echo '$VAR'

# 4  
Old 09-20-2015
Code:
VAR="2  spaces"
VAR2='2  spaces'
echo " --------- "
echo $VAR
echo $VAR2
echo " --------- "
echo "$VAR"
echo "$VAR2"

However, single quotes will just print the variable:
Code:
echo '$VAR $VAR2'

Its just about the quotes, and in this case - all plain text - it doesnt even matter which quote you take, but to preserve formatting, always use quotes.

Hope this helps
# 5  
Old 09-23-2015
Thank you very much for your help.

Double quotes solved my problem...
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Deleting newline and making output in single line with spaces

HI I have a file line vi Input 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 (7 Replies)
Discussion started by: Priya Amaresh
7 Replies

2. Shell Programming and Scripting

making find/sed to include directory names with spaces

how can i make find/sed to include directory names with spaces the command is like this for i in `find wp-content/themes -type f -print0 | xargs -0 grep -l -iE 'e'`;do sed -i -e 's/word1/word2/gI' "$i";done but it skips one directory names with spaces sed: can't read ./Nova: No such... (5 Replies)
Discussion started by: vanessafan99
5 Replies

3. Programming

Trouble with C

Hey, i am having a problem First, i know java well and i have used C++ on occasion so i thought i would be able to deal with a class where they program in C. unfortunately i have hit some speed bumps that i am having problems. Here is my problem: I have a structure cache_t in the sample... (0 Replies)
Discussion started by: zephoid
0 Replies

4. Shell Programming and Scripting

trouble making a useradd script

i'm new to scripting in unix and am trying to make a script to add a user and an encrypted password for them. this is what i have and it isn't giving me any errors, but when i try to login with the new user, the password doesn't work. i'm hoping someone can point me in the right direction ... (1 Reply)
Discussion started by: patt4179
1 Replies

5. Shell Programming and Scripting

Removing blank spaces, tab spaces from file

Hello All, I am trying to remove all tabspaces and all blankspaces from my file using sed & awk, but not getting proper code. Please help me out. My file is like this (<b> means one blank space, <t> means one tab space)- $ cat file NARESH<b><b><b>KUMAR<t><t>PRADHAN... (3 Replies)
Discussion started by: NARESH1302
3 Replies

6. UNIX for Dummies Questions & Answers

how to append spaces(say 10 spaces) at the end of each line based on the length of th

Hi, I have a problem where I need to append few spaces(say 10 spaces) for each line in a file whose length is say(100 chars) and others leave as it is. I tried to find the length of each line and then if the length is say 100 chars then tried to write those lines into another file and use a sed... (17 Replies)
Discussion started by: prathima
17 Replies

7. UNIX for Dummies Questions & Answers

The trouble about SU ...

Hi all, having read lots of posts about SU I don't quiet understand this : I'm doing regular backups of my database (u betta do) and therefore use su - username -c "sqlscript special data_base" in a unixscript which is even using cron. (yep!) Now I need some other script, still with this... (4 Replies)
Discussion started by: nulnul7
4 Replies

8. Shell Programming and Scripting

Strip leading and trailing spaces only in a shell variable with embedded spaces

I am trying to strip all leading and trailing spaces of a shell variable using either awk or sed or any other utility, however unscuccessful and need your help. echo $SH_VAR | command_line Syntax. The SH_VAR contains embedded spaces which needs to be preserved. I need only for the leading and... (6 Replies)
Discussion started by: jerardfjay
6 Replies

9. What is on Your Mind?

The trouble with...

Welcome to "The trouble with...." with your host, ZazzyBob. Todays offering - "The trouble with letting other people host your website" I use a certain web hosting service, who shall of course remain nameless here. They are running PHP 4.3.10 I decide to write a script to test their PHP... (6 Replies)
Discussion started by: zazzybob
6 Replies
Login or Register to Ask a Question