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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:20:28+00:00 2026-06-09T15:20:28+00:00

This function works: source foo.bash && foo -n a b c.txt The problem is,

  • 0

This function works:

source foo.bash && foo -n "a b c.txt"

The problem is, no matter what I’ve tried, I couldn’t get the last line echo "$CMD" (or echo $CMD) to generate exactly this output:

cat -n "a b c.txt"

How to achieve that?

# foo.bash
function foo() {
    local argv=("$@");
    local OUT=`cat "${argv[@]}"`
    local CMD=`echo cat "${argv[@]}"`
    echo "--------------------------"
    echo "$OUT"
    echo "--------------------------"
    echo "$CMD"
}

The output is instead:

cat -n a b c.txt

With this command: foo -n \"a b c.txt\" it does work for the display of the command, but it gives errors for the execution via the backtick.

The file "a b c.txt" is a valid, small, text file.

  • 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-09T15:20:30+00:00Added an answer on June 9, 2026 at 3:20 pm

    There you go, with the help of number of tokens in bash variable I’ve come up with the right solution.

    I’ve almost forgot WHY we actually need quoting for one argument, it’s because it has multiple words!

    function foo() {
        local argv=( "$@" );
        local OUT=`cat "${argv[@]}"`
        echo "--------------------------"
        echo "$OUT"
        echo "--------------------------"
        local CMD="cat"
        for word in "${argv[@]}"; do
            words="${word//[^\ ]} "
            if [[ ${#words} > 1 ]]; then
                local CMD="$CMD \"${word}\""
            else
                local CMD="$CMD $word"
            fi
        done
        echo "$CMD"
    }
    

    Hope it helps someone.

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

Sidebar

Related Questions

This function works fine: std::string get_str() { return std::get<0>( make_tuple(std::string(hi)) ); } But if
This function works as it should, window.setInterval(function(){ var active = $('#frontpageControls a.active'); var next
This function works fine if I disable errors. But I have to solve them.
Can someone elaborate how this function works? Does it load every time from disk
This function only works once, when I click an anchor element again, nothign happens.
Hi am using this function that works by a single ID ( 52000121 )
I do not see how this recursive function works: function f(n) { g(n-1) }
This matrix transposition function works, but I'm trying to understand its step by step
I have this jquery function that works, except I need to add something. I
I am using this jquery function that works fine in win/mac FF 3.5 and

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.