MOHAMED
MOHAMED

Reputation: 43518

How to add a script call to a linux command?

I want to add in my linux system a script call to the ls command. This script should be executed each time the user execute the ls command.

I tried 2 solutions but both are limited:

1) Using alias

alias ls="/root/myscript.sh; ls"

But this solution is limited because the user can call ls via a variable in this way

var="ls"
$var

see this link for more details

2) Using function

I create a function with the name ls:

ls() { /root/myscript.sh; /bin/ls $@ }

But this solution is limited because the user can call ls in this way:

/bin/ls

Are there another solution?

Upvotes: 1

Views: 132

Answers (2)

Iharob Al Asimi
Iharob Al Asimi

Reputation: 53006

You can put a script ls say in ${HOME}/myls/ls and then set the path variable to ${HOME}/myls:$PATH

Upvotes: 1

Eun
Eun

Reputation: 4178

You could always rename /bin/ls to /bin/something and create a shell script for /bin/ls and call the original there.

But be warned this can easly brick your system.

Upvotes: 1

Related Questions