Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using redirection in a linux shell script which run as sudo

Tags:

linux

shell

I have a unix shell script which uses redirection of stdout and stderr to a log file. For example test.sh:

ls -l &>> test.log

My problem is that when I run the script with sudo:

sudo ./test.sh

The & is interpreted as "run in background".

Any suggestions?

like image 883
Itamar Katz Avatar asked May 24 '26 17:05

Itamar Katz


1 Answers

I guess, that your script test.sh does not have a shebang in the first line like this:

#!/bin/bash

Without this line several environment settings affect the way your script is executed. In your case this means that another shell like ash, ksh, dash or whatever will be used to execute the script due to the setting of the root user.

like image 52
A.H. Avatar answered May 27 '26 19:05

A.H.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!