Comments (6)
No, I don't think so. Function executions via the precmd-hook are associated with the primary prompt, which comes into play after the whole command line has been executed. Your function just runs the command line as normal, but cmdpath
is always empty. Replace the body with echo "$1"
and then you can see that precmd doesn't take an argument at all.
from bash-preexec.
The use of a preexec-hook for this task is really complicated, because you must work with extdebug
(?!). Especially, when the command line is not a one-word simple command or contains command subsitution. I would rather use aliases:
for p in {/usr,}/sbin/?*;
do
alias ${p##*/}="sudo $p ";
done;
from bash-preexec.
Okay, thanks for clarifying!
from bash-preexec.
@d630 thanks for providing support. Closing, feel free to reopen if needed.
from bash-preexec.
By the way, it's easier to replace/substitute the command line using a preread-hook
in bpx. But parsing of complicated lines is still very tricky. And there is no thing like "substitute pipeline 3, only if pipeline 2 has failed".
Exp1:
$ shopt -u expand_aliases;
$ function preread { READLINE_LINE="ls / | head -n 1"; };
$ preread_functions=(preread);
$ ls
bin
$ history 2
4 ls / | head -n 1
5 history 2
Exp2:
$ unalias FOO;
$ alias 'ls=ls -1'
$ function preread { READLINE_LINE=${rl0//FOO/"ls / | head -n 1"}; }
$ preread_functions=(preread);
$ echo; FOO; echo; echo $(FOO)
bin
bin
$ history 2
4 echo; ls / | head -n 1; echo; echo $(ls / | head -n 1)
5 history 2
Exp3:
$ alias 'ls=/bin/ls / | head -n 1';
$ unalias head;
$ function preread { __bpx_set_rl2; READLINE_LINE=${rl2[@]}; };
$ preread_functions=(preread);
$ ls; ls && ls
bin
bin
bin
$ history 2
4 /bin/ls -1 / | head -n 1; /bin/ls -1 / | head -n 1 && /bin/ls -1 / | head -n 1
5 history 2
from bash-preexec.
For others who are working on this and want a bash + zsh solution, I wrote this ugly hack based on @d630's first message:
for x in $(echo "$PATH" | tr ':' ' '); do
for p in $x/?*; do
name="${p##*/}"
alias "$name"="echo $name"
done
done
Example:
[~] echo hello world
echo hello world
[~] whoami
echo whoami
[~] id
echo id
[~] echo "this is a test"
echo this is a test
[~] printf "%s" "asdf"
echo printf %s asdf
In a way it works?
from bash-preexec.
Related Issues (20)
- Return immediately when running in Zsh HOT 3
- [Bug] HISTCONTROL ignore* setting should be respected HOT 7
- `__bp_preexec_invoke_exec "$_"` printed when running multiple commads
- Tests fail with bats 1.5.0 HOT 7
- official way to detect whether bash-preexec is loaded HOT 2
- Line number exit outpu command ? HOT 1
- Bash 5.1 supports making PROMPT_COMMAND an array HOT 5
- ShellCheck highlights several issues with the file - Shellcheck could be disabled, or issues resolved HOT 2
- post_cmd functionality addition HOT 2
- Functions called in opposite order in midnight commander subshell HOT 2
- Some tests fail with bats 1.9 HOT 6
- preexec() does not work where as precmd() is working as intended. HOT 1
- Library Authors documentation can still lead to issues HOT 2
- preexec $1 is missing some commands HOT 1
- detailed bash log with preexec and precmd HOT 2
- Not working on Windows using Bash in MSYS2 HOT 7
- preexec doesn't work for no-op command `:`
- `bash-preexec` will cause `hishtory` to be particularly time-consuming for commands including pipeline.
- New release when?
- Run preexec for commented out commands HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bash-preexec.