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

  • SEARCH
  • Home
  • 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 7535217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:15:21+00:00 2026-05-30T06:15:21+00:00

In one of my bash scripts I have this variable, containing a list of

  • 0

In one of my bash scripts I have this variable, containing a list of directories to be excluded from tar, plus their parent directory to be processed:

EXLIST="\
 --exclude='/data/sub1/*' \
 --exclude='/data/sub2/*' \
 --exclude='/data/sub3/*' \
 /data \
 "

echo ${EXLIST} | /usr/bin/xargs -0 tar -cf _data.tar

However, tar cowardly refuses to create an empty archive because what it really receives after substituting ${EXLIST} is:

echo --exclude='/data/sub1/*' | /usr/bin/xargs -0 tar -cf /home/_data.tar

Which tells me that newlines are getting in the way?

I could of course define EXLIST as one long line but I prefer not to, as this makes the list less readable.

Is there a way, in bash, to “flatten” the lines to a string, so that tar can process it?

  • 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-30T06:15:22+00:00Added an answer on May 30, 2026 at 6:15 am

    You should use an array in this case, without xargs.

    EXLIST=("--exclude='/data/sub1/*'" \
            "--exclude='/data/sub2/*'" \
            "--exclude='/data/sub3/*'" \
            "/data")
    
    tar -cf _data.tar "${EXLIST[@]}"
    

    Using this method, each element of the array is a separate argument for tar.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I made this bash one-liner which I use to list Weblogic instances running along
I have a number of shell scripts that all look like this: #!/bin/bash cd
In bash one can escape arguments that contain whitespace. foo a string This also
When working an interactive bash session, one aspect from the Windows shell I miss
This bash script concatenates the names for jar files to a classpath (variable CP),
How can I retrieve a bash variable value if I have the variable name
I have a Bash script that basically initializes an application and sets parameters. One
I have a bunch of scripts - some in perl and some in bash
I have a bash script runned every day via cron. In this bash I
I have several files containing this line Release: X I want to increment X

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.