Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7698131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:10:14+00:00 2026-05-31T22:10:14+00:00

I have the following bash script: #DIR is something like: /home/foo/foobar/test/ without any whitespace

  • 0

I have the following bash script:

#DIR is something like: /home/foo/foobar/test/ without any whitespace but can also include whitespace
DIR="$( cd "$( dirname "$0" )" && pwd )"
#backup_name is read from a file
backup_name=FOOBAR
date=`date +%Y%m%d_%H%M_%S`
#subdirs is also read from the same file
subdirs=etc/ sbin/ bin/
filename="$DIR/Backup_$backup_name"_"$date.tar.gz"

cd /
echo "filename: $filename"
echo "subdirs $subdirs"
cmd='tar czvf "'$filename'" '$subdirs
echo "cmd tar: $cmd"
$cmd

But I get following output:

filename: /home/foo/foobar/test/Backup_FOOBAR_20120322_1529_35.tar.gz
subdirs: etc/ sbin/ bin/
cmd tar: tar cfvz "/home/foo/foobar/test/Backup_FOOBAR_20120322_1529_35.tar.gz" etc/ sbin/ bin/
etc/
# ... list of files in etc
# but no files from sbin or bin directory
tar: "/home/foo/foobar/test/Backup_FOOBAR_20120322_1529_35.tar.gz": can open not execute: File or directory not found
tar: not recoverable error: abortion.

However, when I copy the echo output of the tar command, make a cd to / and paste it into the bash shell it is working:

tar cfvz "/home/foo/foobar/test/Backup_FOOBAR_20120322_1529_35.tar.gz" etc/ sbin/ bin/
etc/
  • Every variable is defined and there is no trailing newline
  • I also tried $cmd with backticks
  • the two variables: backup_name and subdirs are read from a file (I did not include the reading process in the code)

edit: I just copied my script to a dir with no whitespace and changed the line:

cmd='tar czvf "'$filename'" '$subdirs
#to
cmd="tar czvf $filename $subdirs"

and it’s working now but when I do the same in a dir which also contents whitespaces I get still the same error.

edit2: reading from file (the file is read before anything else happens)

config="config.txt"
local line
while read line
do
    #points to next free element and declares it
    config_lines[${#config_lines[@]}]=$line
done <$config
backup_name=${config_line[0]}
subdirs=${config_line[1]}

What is wrong with my bash script?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T22:10:15+00:00Added an answer on May 31, 2026 at 10:10 pm

    Short answer: see BashFAQ #050: I’m trying to put a command in a variable, but the complex cases always fail!.

    Long answer: embedding quotes in a variable doesn’t do anything useful, because when you use it (i.e. $cmd), bash parses quotes before replacing variables; by the time the quotes are there, it’s too late for them to do any good. You do, however, have several options:

    1. Don’t bother with putting the command in a variable in the first place, just use it directly:

      echo "filename: $filename"
      echo "subdirs $subdirs"
      tar czvf "$filename" $subdirs
      
    2. If you really need to put it in a variable first, use an array rather than a plain text variable (and ideally, do the same with the subdirs list):

      subdirs=(etc/ sbin/ bin/)
      ...
      
      echo "filename: $filename"
      echo "subdirs ${subdirs[*]}"
      cmd=(tar czvf "$filename" "${subdirs[@]}")
      printf "cmd tar:"
      printf " %q" "${cmd[@]}" # Have to do some trickery to get it printed right
      printf "\n"
      "${cmd[@]}"
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to do following: I have a bash script that calls an
I have a bash script that looks like the following: #!/bin/bash FILES=public_html/*.php # */
Say I have the following Bash script stored in the file foo.sh : #!/bin/bash
I have written the following function in a bash script but it is not
hi I have following propertie file (something.properties) SERVER1_PROPERTY1=123 SERVER1_PROPERTY2=${SERVER1_PROPERTY1}/123 and following bash script fetching
I have a shell script file (run.sh) that contains the following: #!/bin/bash %JAVA_HOME%/bin/java -jar
I have the following snippet in a bash script written in Solaris 10: printf
I have the following bash script, that lists the current number of httpd processes,
I have attempted to run the following bash script on my internet tablet (Nokia
I have the following snippet in a bash script: db2 connect to $DB var=$(db2

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.