Git Product home page Git Product logo

Comments (6)

d630 avatar d630 commented on July 24, 2024 1

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.

d630 avatar d630 commented on July 24, 2024

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.

ageis avatar ageis commented on July 24, 2024

Okay, thanks for clarifying!

from bash-preexec.

rcaloras avatar rcaloras commented on July 24, 2024

@d630 thanks for providing support. Closing, feel free to reopen if needed.

from bash-preexec.

d630 avatar d630 commented on July 24, 2024

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.

Arinerron avatar Arinerron commented on July 24, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.