git rebase x run an alias - terminal

I'm trying to exec an alias while doing a git rebase -i
pick hash commit_message
x alias_name
The error:
Executing: alias_name
error: cannot run alias_name: No such file or directory
warning: execution failed: alias_name
You can fix the problem, and then run
git rebase --continue
alias alias_name='calls a phyton script to run arc command'
If I do run the alias on the terminal, it works as expected.

Git doesn't know about any aliases or shell functions you've defined, so the shell it starts to execute the given command won't know about them either. You'll need to source a file containing the definition of the alias, then enable alias expansion if necessary, in the command itself.
pick hash commit_message
x source some_file; shopt -s expand_aliases; alias_name
where some_file contains your alias definition.
I suspect, though, that this will still fail, because the shell in question will try to expand aliases in that full command line before it actually sources the file or enables alias expansion, leaving alias_name an undefined command name. Replace the alias with a function should work, though.
With
func_name () {
the_script.py arg1 arg2
}
in an accessible file, do
x source some_file; func_name

Related

replace rm with gio trash [duplicate]

I am trying to use the solution of using sudo on my existing aliases as covered in many existing answers already and i am so confused as to why it is not working
alias sudo='sudo '
I keep all my aliases in .bash_aliases. Inside .bash_aliases I have this
function _test {
echo 'test!'
}
alias test='_test'
I reload the .bashrc each time; source .bashrc but when I run sudo test I always get
sudo: _test: command not found
The only strange thing that happens is that I get the following on reload and not on a new terminal
dircolors: /home/MYHOME/.dircolors: No such file or directory
but i feel this is a red herring.
As l0b0 says, aliases cannot be used in this way in bash.
However, you can pass a function through (and really, there's basically never a good reason to use an alias instead of sticking to functions alone).
_test() {
echo 'test!'
}
sudo_test() {
sudo bash -c "$(declare -f _test)"'; _test "$#"' sudo_test "$#"
}
...will define a command sudo_test that runs the function _test via sudo. (If your real-world version of this function calls other functions or requires access to shell variables, add those other functions to the declare -f command line, and/or add a declare -p inside the same command substitution to generate a textual description of variables your function needs).
To run an alias like alias_name it must be exactly the first word in the command, so sudo alias_name will never work. Ditto 'alias_name', \alias_name and other things which eventually expand to the alias name.

How to make an alias not immediately execute

I'm adding some aliases to my .zshrc file and wondering if I can add an alias that doesn't execute immediately?
Example of usage I want:
I want to be able to have an alias for creating a new git branch
Ideally I can type the alias gcb which will output git checkout -b in the terminal and doesn't execute, then I can add the new branch name and hit enter.
Note:
I know I can do the following:
command alias_name to prevent the alias immediately executing, but I'm seeking a way to do this without the command keyword as having to type that extra text almost defeats the purpose of the alias shortcut
Thanks!
A definition
alias foo=bar
immediately defines the alias foo in the current zsh process. If you later type
foo x y
the command bar x y is executed. Note that by definition of zsh, this behaviour only works in interactive shells. If you want to exploit this in a shell script, you have to enable alias expansion explicitly.

bash alias using sudo returning command not found

I am trying to use the solution of using sudo on my existing aliases as covered in many existing answers already and i am so confused as to why it is not working
alias sudo='sudo '
I keep all my aliases in .bash_aliases. Inside .bash_aliases I have this
function _test {
echo 'test!'
}
alias test='_test'
I reload the .bashrc each time; source .bashrc but when I run sudo test I always get
sudo: _test: command not found
The only strange thing that happens is that I get the following on reload and not on a new terminal
dircolors: /home/MYHOME/.dircolors: No such file or directory
but i feel this is a red herring.
As l0b0 says, aliases cannot be used in this way in bash.
However, you can pass a function through (and really, there's basically never a good reason to use an alias instead of sticking to functions alone).
_test() {
echo 'test!'
}
sudo_test() {
sudo bash -c "$(declare -f _test)"'; _test "$#"' sudo_test "$#"
}
...will define a command sudo_test that runs the function _test via sudo. (If your real-world version of this function calls other functions or requires access to shell variables, add those other functions to the declare -f command line, and/or add a declare -p inside the same command substitution to generate a textual description of variables your function needs).
To run an alias like alias_name it must be exactly the first word in the command, so sudo alias_name will never work. Ditto 'alias_name', \alias_name and other things which eventually expand to the alias name.

Why can't I 'cd' into Bash aliases on Cygwin?

I'm using Bash via the mintty terminal on Cygwin, and I've created two aliases in my .bashrc file in my Cygwin home directory.
alias croot="C:/cygwin64"
alias desktop="B:/Users/User/Desktop"
When I enter croot or desktop into the terminal, it seems to work fine:
B:/Users/User/Desktop: Is a directory
However, using those aliases with something like cd croot returns the error:
-bash: cd: croot: No such file or directory
What's going on here?
alias doesn’t work the way you think it does. Do this:
alias croot='cd C:/cygwin64'
croot
Or:
croot=C:/cygwin64
cd "$croot"
Result:
$ pwd
/
There is a way to make this work. But I would not recommend it. Use steven's answer instead.
$ help alias
alias: alias [-p] [name[=value] ... ]
Define or display aliases.
Without arguments, 'alias' prints the list of aliases in the reusable
form 'alias NAME=VALUE' on standard output.
Otherwise, an alias is defined for each NAME whose VALUE is given.
A trailing space in VALUE causes the next word to be checked for
alias substitution when the alias is expanded.
Options:
-p print all defined aliases in a reusable format
Exit Status:
alias returns true unless a NAME is supplied for which no alias has been
defined.
$ alias croot="C:/cygwin64"
$ alias desktop="B:/Users/User/Desktop"
$ alias cd='builtin cd ' # Notice the trailing space.
$ cd croot; pwd
/
Note that only the word immediately next to cd will be considered for alias expansion. Hence cd -P croot will not work.

Prevent a command in a shellscript from being executed inside another shellscript

I am invoking a shellscript inside another shellscript, and the invoked one has a command to delete a folder, which I don't want to be executed, like this
$ rm ../temp -rf
Is there a way to prevent this command from being executed without changing the invoked script contents?
You can define an alias like this in your script:
alias rm='echo SAFE'
By default aliases work only in interactive shells, so you must change this behaviour with:
shopt -s expand_aliases
in your script and source the other script (the one you cannot change):
source the_other_script.sh
or
. the_other_script.sh
This will work unless the other script runs rm in a sub-shell.
Another, safer method is to create your own rm command which does nothing (or just prints out its argument so you know what's going on), put it into a directory and put this directory as the first one in the PATH environment variable like this (Korn shell syntax):
$ export PATH=/path/to/your/dummy/rm/replacement:$PATH

Resources