Saturday, October 29, 2022

[SOLVED] Make a Bash script echo executed commands

Issue

I put together a bunch of alias commands in a folder. Mainly ssh, so instead of having to type...

ssh [user]@[server]

...ten times a day, I can just type...

server

...and the alias script fires. The script is simply the command...

ssh [user]@[server]

This is all working fine, but I was wondering if there was a way in Bash where instead of firing the ssh command quietly, it would display the command that is being executed?


Solution

You can debug the script with -x, which will echo the commands and arguments.

bash -x script.sh

You can do this for specific portions of the file, too (from section 2.3.2 linked above):

set -x          # activate debugging from here
ssh [email protected] ...
set +x          # stop debugging from here

The output from -x might be a bit too verbose for what you're looking for, though, since it's meant for debugging (and not logging).

You might be better off just writing out your own echo statements - that'd give you full control over the output.



Answered By - Rob Hruska
Answer Checked By - Dawn Plyler (WPSolving Volunteer)