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 8441007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:27:10+00:00 2026-06-10T08:27:10+00:00

I am writing a bash script that will output a .tgz file to a

  • 0

I am writing a bash script that will output a .tgz file to a specific directory, /tmp/ by default
I would like to provide an option to override this directory and I have chosen to do so using arguments provided at the command line

    while getopts d: option
    do
    case "${option}" in
    d) dir=${OPTARG};;
    esac
    done

As written, this works but I’ve run into a snag depending on user input
The name of my .tgz file is also a variable and my code that brings this all together is

    output="$dir""$name"

The problem that I run into is if the user runs

    ./script -d /home/user

My resulting path and filename end up as

    /home/userfilename.tgz

I need to either enforce a requirement for a trailing / or insert one if the user did not.
While it works, if I change my output variable to

    output="$dir"/"$name"

If the user does provide a trailing / I end up with something like this and I am trying to keep my output aesthetic.

    /home/user//filename.tgz

Any input would be greatly appreciated.

  • 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-06-10T08:27:12+00:00Added an answer on June 10, 2026 at 8:27 am

    Add the line

    output="${output//\/\///}"
    

    after joining dir and name.

    It looks complicated, but what it does is it replaces two slashes with one.

    You may find more info in here.

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

Sidebar

Related Questions

I'm writing a bash script that will (hopefully) redirect to a file whose name
I am writing some BASH shell script that will continuously check a file to
I am writing a bash script that modifies a file that looks like this:
I'm writing a bash script that encrypts the data of a folder or file
I'm writing a bash script that will receive a password via STDIN and go
I am writing a bash script that, when run from directory B , mirrors
I am writing a bash script for an automator service that will take a
I'm writing a bash script that spawns a number of background processes. I'd like
I'm writing a script right now in Bash that will execute two SQL queries
I'm writing a bash script that will download the page then search for a

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.