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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:36:27+00:00 2026-05-31T16:36:27+00:00

How many times have you seen someone trying to Log the command I run

  • 0

How many times have you seen someone trying to “Log the command I run and the output of the command”? It happens often, and for seeing the command you’re running set -v is nice, (set -x is nice, too, but it can be harder to read), but what happens when you want to capture the command being run… but not all commands being run?

Running interactively I don’t see a way to capture the set -v output at all.

set -v
echo a 1>/dev/null # 'echo a 1>/dev/null' is printed to the screen
echo a 2>/dev/null # 'echo a 2>/dev/null\na' is printed to the screen

I can put this in a script and things get better:

echo 'set -v'$'\n''echo a' > setvtest.sh

bash setvtest.sh 1>/dev/null # 'echo a' is printed to the screen
bash setvtest.sh 2>/dev/null # 'a' is printed to the screen

Aha, so from a script it goes to stderr. What about inline?

set +v
{ set -v ; echo a ; } 1>/dev/null # no output
set +v
( set -v ; echo a ; ) 1>/dev/null # no output

Hmm, no luck there.

Interestingly, and as a side note, this produces no output:

echo 'set -v ; echo a' > setvtest.sh
bash setvtest.sh 1>/dev/null

I’m not sure why, but perhaps that’s also why the subshell version returns nothing.

What about shell functions?

setvtest2 () {
    set -v
    echo a
}

setvtest2               # 'a'
set +v
setvtest2 1>/dev/null   # nothing
set +v
setvtest2 2>/dev/null   # nothing

Now the question: Is there a nice way to capture the output of set -v?

Here’s my not-nice hack, so I’m looking for something less insane:

#!/usr/bin/env bash

script=/tmp/$$.script
output=/tmp/$$.out

echo 'set -v'$'\n'"$1" >"$script"

bash "$script" 1>"$output"
cat "$output"
rm -f "$script" "$output"

Now I can execute simple scripts

bash gen.sh 'echo a' 1>/dev/null # prints 'echo a'
bash gen.sh 'echo a' 2>/dev/null # prints 'a'

But surely there are better ways.

  • 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-31T16:36:28+00:00Added an answer on May 31, 2026 at 4:36 pm

    You can run bash with option -v instead of turning on and off via set:

    bash -v -c "echo a" 1>/dev/null # prints 'echo a'
    bash -v -c "echo a" 2>/dev/null # prints 'a'
    

    The dark side of this solution is that each such line will require to create new bash process, but you will not have to remember to switch off the v option back, since it’s switched on only in a child process.

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

Sidebar

Related Questions

I have seen in the output-Window of Visual Studio, that there occurs many times
I have searched through many times but have not seen this before. Probably really
I have seen in many times in JavaScript code people add a return true
Greetings everybody. I have seen examples of such operations for so many times that
I have done this many times and have never seen this. I have an
How many times have we seen this type of selector: List Box Selector http://geekswithblogs.net/images/geekswithblogs_net/dotNETvinz/MoveItemsListBox.jpg
I have seen many times things like this: function customArrayIndexOf(item, array){ for (var i
I have seen this kind of code many times before, most recently at scala-user
Many times I have seen Visual Studio solutions which have multiple projects that share
Many times I have seen code where they write a Class like this: public

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.