Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make bash script echo executed command

Tags:

bash

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?

like image 457
scphantm Avatar asked Mar 22 '12 13:03

scphantm


People also ask

Does echo execute command?

Explanation: echo will print the command but not execute it. Just omit it to actually execute the command.

What is echo $$ in bash?

The echo command is used to display a line of text that is passed in as an argument. This is a bash command that is mostly used in shell scripts to output status to the screen or to a file.


1 Answers

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.

like image 137
Rob Hruska Avatar answered Sep 19 '22 00:09

Rob Hruska