bash: How to intercept command line and do various

2019-08-23 12:00发布

问题:

I would like to know a solution to the following problem.

Problem: When I enter a number/string on command line, I should be able to interpret it in my own way, and do any action based on the contents of the string e.g. if it is a number then do action1, else do action 2, etc.

Example:

$ 85049  
[ This should do 'cd /dir1/dir2/../85049' ]

I cannot make an alias for this, as I can enter any big number.

Any ideas, how to achieve this functionality via bash?

回答1:

You can use the command_not_found_handle function.

http://www.gnu.org/software/bash/manual/bashref.html#Command-Search-and-Execution

I don't think that's a very good idea though and would suggest that you rethink your original premise.



标签: bash shell unix