InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

Bash shell scripting

  • Bash - add a number to a variable
  • Bash - append text to a variable
  • Bash - how to check if a variable is set
  • Bash - how to compare file timestamps
  • Bash - how to find last command exit status code
  • Bash - how to get main program and current file dir location
  • Bash - how to redirect stderr to stdout or file
  • Bash - how to run custom commands at script exit
  • Bash - how to stop at error
  • Bash - how to use functions - quick tutorial
  • Bash - iterate over array
  • Bash - local and global variables
  • Bash - newline and other escape character in string
  • Bash - pass all arguments from one script to another
  • Bash - set default value if a variable is empty
  • Bash - variables in double quotes vs without quotes
  • Bash associative array tutorial
  • Bash check if file begins with a string
  • Bash shell - check if file or directory exists
  • Can global variables be modified in bash function?
  • Find memcache request hit rate on linux command line
  • How to return a value from bash function
  • Iterate over specific file extension in a dir in shell script
  • Linux - Yesterday's Date in YYYYMMDD format
  • bash - extract urls from xml sitemap
  • bash - how to use regex in if condition
 
  • Home
  • > Tutorials
  • > Bash shell scripting

Bash – variables in double quotes vs without quotes

By admin on Dec 15, 2015

Bash variables without double quotes can give surprising results. The special characters inside a variable when not double quoted, gets interpreted by bash. Few points to note:

  1. echo will pint newline as space if variable is not quotes.
  2. bash for loop will treat a string with spaces/newlines as multiple values delimited by space.

Here are some code examples

ls outcome – echo variable with quote vs without quote

#!/bin/bash
mkdir -p testdir
cd testdir; touch file1.txt; touch file2.txt
a=$(ls)
echo '===echo $a [newlines not expected]==='
echo $a
echo '===echo "$a" [newlines expected]==='
echo "$a"
===echo $a [newlines not expected]===
file1.txt file2.txt
===echo "$a" [newlines expected]===
file1.txt
file2.txt
Env: GNU bash, version 4.2.46

ls outcome – for loop iteration on variable with quote vs without quote

#!/bin/bash
mkdir -p testdir
cd testdir; touch file1.txt; touch file2.txt
a=$(ls)
echo '===iterating over $a==='
for i in $a
do
  echo "val: $i"
done
echo '===iterating over "$a"==='
for i in "val: $a"
do
  echo "$i"
done
===iterating over $a===
val: file1.txt
val: file2.txt
===iterating over "$a"===
val: file1.txt
file2.txt
Env: GNU bash, version 4.2.46

Suggested posts:

  1. Bash – how to get main program and current file dir location
  2. Docker quick start guide on Ubuntu
  3. Why and how to use Amazon route53 as DNS
  4. Install Amazon EC2 api tools on Ubuntu Linux
  5. Can global variables be modified in bash function?
  6. Can ssh keys generated as one user be used as a different user
  7. How to specify environment variable for a command on Linux
  8. Bash – iterate over array
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged Bash shell scripting, Tutorials
  • Browse content
  • Article Topics
  • Article archives
  • Contact Us
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

Copyright © 2025 InfoHeap.

Powered by WordPress