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

The Archive Base Latest Questions

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

The following code works fine #!/bin/bash while true; do echo $(pwd | awk ‘{print

  • 0

The following code works fine

#!/bin/bash

while true; do
     echo $(pwd | awk '{print "dir: "$1}')
     sleep 1
done

But I need the command to be in a variable, like this.

#!/bin/bash                                                                     

COMMAND=pwd | awk '{print "dir: "$1}'
while true; do
     echo $($COMMAND)
     sleep 1
done

The first snippet executes fine, and prints something like dir: /present/directory. The second snippet just prints blank lines. Why is that and how do I fix it?

There’s no significance in the commands I used. I just needed to use a pipe and chose these commands to demonstrate 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-06-10T08:58:21+00:00Added an answer on June 10, 2026 at 8:58 am

    If you like living dangerously, the answer is eval:

    #!/bin/bash                                                                     
    
    COMMAND="pwd | awk '{print \"dir: \"\$1}'"
    while true
    do
        echo $(eval $COMMAND)
        sleep 1
    done
    

    Why dangerously? It’s hard to control what’s going on with eval, doubly so if you accept the command string from a naïve (let alone malicious) user. Also note how carefully I had to quote the string containing the command.

    Why didn’t version 2 in the question work?

    COMMAND=pwd | awk '{print "dir: "$1}'
    

    This line doesn’t do what you think it does, put bluntly.

    It is a pipeline, with the first ‘command’ being the string assignment COMMAND=pwd treated as an environment variable for the empty command, which sends no data to awk. After completion, the value in $COMMAND is an empty string (both because of the sub-shell used with the pipeline and because the COMMAND=pwd only applied while the (empty) command was executed), so the loop echoes the result of executing the empty string — which is also an empty string. Hence the line of blanks.


    This works for me but I’m having an issue with adding a tab in the AWK printout, and I think it’s because of the escaping. If I have COMMAND="ls -l | awk '{print \$8 \$9}'", how would I insert a tab \t between the $8 and $9?

    Ouch, and Yuck!

    If you had a tab in the source between $8 and $9, awk would treat it as generic white space and paste $8 and $9 together in the output. To get a tab in the output, you’ll need either a quoted string containing a tab, or a quoted string containing \t in between the two fields, or you’ll use a printf() with a suitable format string.

    So, in principle, you’ll probably want:

    COMMAND='ls -l | awk '\''{printf "%s\t%s\n", $8, $9}'\'
    

    I’ve switched to single quotes around the string because they’re mostly easier to understand. Whether you use single or double quotes, the command itself contains both, so you have to worry about how to do the escaping. The '\'' sequence is how you embed a single quote into a single quoted string; the first single quote ends the current single quoted string; the backslash single quote adds a single quote, and the last single quote restarts a single string. The sequence '\' at the end could also be written '\''', but the last two quotes are a zero-length single quoted string, so I dropped them.

    This produces the nice output with tabs; you can verify by running:

    eval $COMMAND
    

    The echo $(eval $COMMAND) completely undoes all the careful spacing, flattening the entire list of files and times into a single line with each ‘word’ separated from the next by spaces.

    You can work around that issue by forcing echo to respect the spacing (and newlines) with:

    echo "$(eval $COMMAND)"
    

    but this raises the question of ‘why not use just eval $COMMAND?’

    (Testing: bash 3.2.48 Mac OS X 10.7.4.)

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

Sidebar

Related Questions

The following works fine on Mac OS X: #!/bin/bash R CMD Sweave myfile.Rnw pdflatex
The following code works fine on Linux but throws an exception on OS X
The following code works fine in firefox but as with many other things, I
The following code works fine: person = {:a=>:A, :b=>:B, :c=>:C} berson = {:a=>:A1, :b=>:B1,
The following code works fine for Visual C++ 2008. However, when comes to Visual
The following code works fine #define open { #define close } #include<stdio.h> #define int
The following code works fine : std::map<int, int>& m = std::map<int, int>(); int i
The following code works fine. class A { private: int _value; public: class AProxy
The following code works fine in most browsers but it won't work in Internet
In chrome and safari, the following code works fine. in firefox , all platforms,

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.