Generally, I am wondering how I can pass the arguments from a terminal command to stdout. More specifically, I am using tar as part of my backup process and I would like the parameters (especially excluded dirs) to be passed to a log file. For the excluded dirs, I tried tar --show-omitted-dirs, but that did not work as I thought.

I am new to working with bash scripts, but I read up about "$@" and figured I could write my own function

myLS() { echo "$@"; ls "$@"; }

to first echo the commands to stdout and then pass them to ls. Piping echo

myLS() { echo "$@" | tr " " "\n"; ls "$@"; }

or using printf

myLS() { printf '%s\n' "$@"; ls "$@"; }

separates these arguments on new lines, which is what I would want on top of the log file.

While this works, I am curious if there is a preferred way to go about it, which does not require me to rewrite my own function every time I want to keep a log of the arguments I used when running a command. Both general solution and solutions specific to tar are helpful.


There is in fact no preferred way to go about doing this. The best way that I can think of, and the method I do use is exactly what you did. Create a function that first prints the paramaters, and then runs the command with those paramaters. The only improvement I can think to make is instead of using a different function for each command, you could write one function such as:

function show_args {
    echo "$@" | tr " " "\n" >> logfile

This function can be called like:

show_args ls -ah ~/documents/

It will echo the arguments stacked on top of eachother to "logfile" and then run the command.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.