How to pass ags to a specific part of an alias - shell

I made my own script to move "up" on unix (cd ..) and it is called like this: $ /path/to/script/myup args | cd where args may be nothing. I would like to alias this to "up" on my tcsh and bash shells so I can call it: $ up args. I thought it would be aliased like: alias up '/path/to/script/myup \!*| cd' because that is what I used for my cdls alias but its not working. How do I properly pass parameters to an alias?

perhaps you mean cd
`/path/to/script/myup\\!*`
This execute myup and passes its output to cd as a parameter. Note that they are backwards quotes

Related

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.

ZSH: Call in-built function from zsh function that uses the same name

I use zsh and would like to slightly extend the in-built cd function.
I'd like that when I call cd, it changes directly and then lists the content of the directory.
function cd() {
cd $1
ls .
}
I'd have expected this code to work, but as it turns out, the call to cd refers to the function definition, resulting in an infinite loop.
Is there a work-around to solve this problem, apart from choosing a different name for my function?
UPDATE
Strangely enough, this worked
function cd() {
`echo $1`
ls .
}
No idea why.
In order to use builtin commands from within functions of the same name, or anywhere else for that matter, you can use the builtin precommand modifier:
function cd() {
builtin cd $1
ls .
}
builtin COMMAND tells zsh to use the builtin with the name COMMAND instead of a alias, function or external command of the same name. If such a builtin does not exist an error message will be printed.
For cases where you want to use an external command instead of an alias, builtin or function of the same name, you can use the command precommand modifier. For example:
command echo foobar
This will use the binary echo (most likely /bin/echo) instead of zsh's builtin echo.
Unlike with functions builtin and command are usually not necessary with aliases to prevent recursions. While it is possible to use an alias in an alias definition
% alias xx="echo x:"
% alias yy="xx y:"
% yy foobar
y: x: foobar
each alias name will only expanded once. On the second occurence the alias will not be expanded and a function, builtin or external command will be used.
% alias echo="echo echo:"
% echo foobar
echo: foobar
% alias xx="yy x:"
% alias yy="xx y:"
% xx foobar
zsh: command not found: xx
Of course, you can still use builtin or command in aliases, if you want to prevent the use of another alias, or if you want to use a builtin or external command specifically. For example:
alias echo="command echo"
With this the binary echo will be used instead of the builtin.
Why the echo command works is because you probably have the autocd option on. You can check this by typing setopt to get your list of options.
Then the echo-ing of the directory name and catching the output triggered the autocd and you went to that directory.

Preventing 'process completed' after bashrc function in OS X

In my .bashrc file, I have the following lines:
alias cd='_cd'
function cd()
{
cd "$1"
PS1='[$USER] "$PWD" $ '
}
However, after sourcing my .bashrc, every time I try to run the command, I get a process completed message, and I am locked out of the shell.
[prompt] $ source ~/.bashrc
[prompt] $ cd ~
[Process completed]
How can I easily implement this function without getting the process completed message?
Your cd function is recursive, and eventually the shell gets too deep and gives up.
Ensure you're calling the shell's cd inside the function:
cd() {
builtin cd "$1"
PS1='[$USER] "$PWD" $ '
}
You don't have to do this if you define your prompt with: PS1='[\u] "\w" \$ ' -- see the PROMPTING section of your bash man page.
Declaring alias cd='_cd' does not mean you are changing the builtin command cd to _cd. It means you are making an alias of _cd that is invoked when you enter cd. Command expansion follows the order of aliases, functions, builtin and then executables in $PATH. So if there is an alias, function and builtin with the same name, the alias will be executed.
Next it seems you are trying to set your PS1 with a function, while as Jonathan explained it is better to just declare it plain in your .bashrc like
PS1='[$USER] "$PWD" $ '
I would recommend however to use the special characters the prompt recognizes instead of system variables.
$USER is the current user, which in PS1 can represented by \u
$PWD is the working directory, you have the option here to show the full path with \w or just the current with \W.
There are a lot of other useful options, but you should check them out by yourself.
https://www.gnu.org/software/bash/manual/bashref.html#Controlling-the-Prompt
So your prompt may be something like PS1=[\u] \w $

How can I escape bash commands in a bash alias

I want to create an alias that will use bash commands like pwd. Like
alias myalias="myprogram $(pwd)".
But defined like this the alias will be evaluated when the alias is loaded and not when I run my alias. How can I achieve that ?
Simply escape with \:
alias myalias="myprogram \$(pwd)"
This results in:
$ alias myalias
alias myalias='myprogram $(pwd)'
and $(pwd) gets evaluated when you run myalias.

Failing to modify .bashrc

My .bashrc file contains:
# mkdir, cd into it
function mkcd ()
{
mkdir -p "$*"
cd "$*"
}
When I type mkcd in shell I get mkcd: Command not found.
when I type source ~/.bashrc I get an error:
Badly placed ()'s.
by the way, my text editor (emacs) is recognising the code as Shell-script[tcsh].
How do I fix this?
If you can accept the restriction that you have to pass the name of the directory to be created as the first argument, it should look like this:
# mkdir, cd into it
function mkcd ()
{
mkdir -p "$#"
cd "$1"
}
You need to run source ~/.bashrc to see it working (or alternatively start a new shell).
Three comments on that function. This will work mostly. To catch some corner cases:
Either use function mkcd { ...; } or mkcd() { ...; }. The first is compatible with ksh, but only if you drop the (). The mkcd() notation is the standard POSIX notation.
Even mkdir -p can fail, so make the cd conditional on mkdir.
Finally, you want exactly one argument to mkdir and cd. Use only one argument, and test that it has a value with the :? modifier in parameter substitution. This will stop the function from sending you $HOME.
Together:
function mkcd
{
mkdir -p "${1:?}" && cd "${1}"
}
Put that in your .bashrc and open a new shell. Type type mkcd. This should respond with:
mkcd is a function, followed by its definition.
I ran your mkcd function on bash 4.2.45 and linux 3.8.0 and it worked as expected. Logging on in a fresh window or running
source ~/.bashrc
in your existing window should define the function for you. If it does not work you'll get an error message like:
mkcd: command not found
While hek2mgl's suggestion is not necessary to make it work, it does make it make more sense since you're only going to cd to one directory.
As commented by Henk Langeveld and hek2mgl: "Wrong shell. Badly placed ()'s is a message produced by tcsh. Switch to bash or ksh."
I thought that opening a terminal on ubuntu entered straight into bash environment. In fact, as commented below, "a Terminal will start a copy of your login shell as defined in /etc/passwd". By typing ps -p $$ in terminal, I realise mine is set to tcsh, the C shell. In that case, one needs to type bash to get into bash environment.
Then source ~/.bashrc compiles the definition for mkcd.

Resources