Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to "hide" an executable from a bash script?

Tags:

bash

shell

I want to test the output of a bash script when one of the executables it depends on is missing, so I want to run that script with the dependency "hidden" but no others. PATH= ./script isn't an option because the script needs to run other executables before it reaches the statement I want to test. Is there a way of "hiding" an executable from a script without altering the filesystem?

For a concrete example, I want to run this script but hide the git executable (which is its main dependency) from it so that I can test its output under these conditions.

like image 247
Sean Kelleher Avatar asked Jun 28 '15 11:06

Sean Kelleher


People also ask

How do I not show output in bash?

To silence the output of a command, we redirect either stdout or stderr — or both — to /dev/null. To select which stream to redirect, we need to provide the FD number to the redirection operator.

How do I stop a bash EXE?

If you are executing a Bash script in your terminal and need to stop it before it exits on its own, you can use the Ctrl + C combination on your keyboard.

Does a bash script need to be executable?

Before being able to run your script, you need your script to be executable. In order to make a script executable on Linux, use the “chmod” command and assign “execute” permissions to the file.

How do I hide user input in terminal?

Using the stty Command The stty command displays or changes the settings of a terminal and is also useful for hiding user input. We can use the echo setting of the stty command for enabling or disabling the echoing of input characters.


2 Answers

You can use the builtin command, hash:

hash [-r] [-p filename] [-dt] [name]

Each time hash is invoked, it remembers the full pathnames of the commands specified as name arguments, so they need not be searched for on subsequent invocations. ... The -p option inhibits the path search, and filename is used as the location of name. ... The -d option causes the shell to forget the remembered location of each name.

By passing a non-existent file to the -p option, it will be as if the command can't be found (although it can still be accessed by the full path). Passing -d undoes the effect.

$ hash -p /dev/null/git git
$ git --version
bash: /dev/null/git: command not found
$ /usr/bin/git --version
git version 1.9.5
$ hash -d git
$ git --version
git version 1.9.5
like image 96
npostavs Avatar answered Sep 19 '22 15:09

npostavs


Add a function named git

git() { false; }

That will "hide" the git command

To copy @npostavs's idea, you can still get to the "real" git with the command builtin:

command git --version
like image 34
glenn jackman Avatar answered Sep 19 '22 15:09

glenn jackman