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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:07:22+00:00 2026-06-15T02:07:22+00:00

I have a bash function that fetches values (using curl and cut) and creates

  • 0

I have a bash function that fetches values (using curl and cut) and creates a file name from them. Now I want to support a second naming scheme that needs a different set of parameters.

Example:

#!/bin/bash

TEMPLATE="%02i. %s.txt"

foo() {
    a="Imagine these to"
    b="be set dynamically"
    c="42"

    filename="$(printf "$TEMPLATE" "$c" "$a")"
    # second: filename="$a - $b.txt"
    # or:     filename="$(printf "%s - %s.txt" "$a" "$b")"
    echo "$filename"
    # generate file
}

# actual script loops over:
foo

One of the values is a number that should be padded with leading zeros if required, thus printf in the current implementation.

Is there a way to implement this with just setting a different template globally? This would require that the template can access parameters by index or at least skip some of them.

If not, what are my alternatives? The template is to be chosen by command line parameter and does not change after initialization.

What does not work:

  • bash man page suggests that zero length output is not possible (to skip values)
  • C’s printf man page mentions a “%m$” construct, which apparently is not supported in bash
  • the function itself generates the values, so it cannot receive the full filename as parameter
  • 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-15T02:07:23+00:00Added an answer on June 15, 2026 at 2:07 am

    If you need to skip an argument, you can use %.s. Examples:

    $ printf "%.s%s\n" "Bonjour" "Hello"
    Hello
    $ printf "%s%.s\n" "Bonjour" "Hello"
    Bonjour
    

    AFAIK, you can’t access arguments by index.

    If you need to store the formated string in a variable, please don’t use a subshell as in:

    $ variable=$(printf "%.s%s" "Bonjour" "Hello")
    $ echo "$variable"
    Hello
    

    Instead, use the -v option to printf (type help printf to have the details) as in:

    $ printf -v variable "%.s%s" "Bonjour" "Hello"
    $ echo "$variable"
    Hello
    

    Also, if your template can come from user input, I would advise you to add -- just before it (this is to end the options of the command, just in case a user wants to start a template with a dash). Hence I would replace you line

    filename="$(printf "$TEMPLATE" "$c" "$a")"
    

    with

    printf -v filename -- "$TEMPLATE" "$c" "$a"
    

    Finally, having upper case variable names is considered bad bash practice.

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

Sidebar

Related Questions

I have an exec function in php file that execs a bash script. It
I have a script that looks like this #!/bin/bash function something() { echo hello
I have a bash script that has this function in it: function start_vi() {
I have a little bash function to log my Macports outputs to a file
I have a bash function that produces some output: function scan { echo output
I have a Unix bash function that executes a script that parses custom environment
I have function that is sourced through the .bashrc file on remote host A.
Greetings, I have a bash script that parses ZIP files we receive from a
Let's say I have a bash function Yadda() { # time-consuming processes that must
I have a bash script file which starts with a function definition, like this:

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.