Linux alias with space

How to cd into a directory with space in the name?

When I manually type it in, the backspace does its escape character thing, but not when I use parameter expansion with the variable DOCS . I tried other variations such as no backslash.

$ DOCS=/cygdrive/c/Users\ dir/Documents $ echo $DOCS /cygdrive/c/Users/my dir/Documents $ cd $DOCS -bash: cd: /cygdrive/c/Users/my: No such file or directory 
$ DOCS="/cygdrive/c/Users/my dir/Documents" $ echo $DOCS /cygdrive/c/Users/my dir/Documents $ cd $DOCS -bash: cd: /cygdrive/c/Users/my: No such file or directory 
$ DOCS="\"/cygdrive/c/Users/my dir/Documents\"" $ echo $DOCS "/cygdrive/c/Users/my dir/Documents" $ cd $DOCS -bash: cd: "/cygdrive/c/Users/my: No such file or directory 

No realy difference if I escape the space using a backslash or quotes, but I did it again anyway. See revision.

18 Answers 18

You need to quote «$DOCS» to prevent spaces from being parsed as word separators. More often than not, variable references should be quoted.

Note that $HOME would have the same problem. The issue is coming from when the shell evaluates variable references; it’s nothing to do with what variables you use or how you assign to them. It’s the expansion that needs to be quoted.

This is deceptive. echo is actually echoing the two strings /home/my and dir . If you use cd or ls you’ll see how it’s actually working.

$ ls $HOME ls: cannot access /home/my: No such file or directory ls: cannot access dir: No such file or directory $ cd $HOME bash: cd: /home/my: No such file or directory $ cd "$HOME"

Can I ask why it works when I manually type it in but not in a variable?

Great question! Let’s examine the commands you typed:

$ DOCS="\"/cygdrive/c/Users/my dir/Documents\"" $ echo $DOCS "/cygdrive/c/Users/my dir/Documents" $ cd $DOCS -bash: cd: "/cygdrive/c/Users/my: No such file or directory 

The reason this doesn’t work is because Bash doesn’t parse quotes inside variable expansions. It does perform word splitting, so whitespace in unquoted variable expansions is taken as word separators. It doesn’t parse quotes in any way, meaning you can’t put double quotes inside a variable to override word splitting.

Because of this, cd is passed two parameters. As far as cd knows it looks like you wrote:

$ cd '"/cygdrive/c/Users/my' 'dir/Documents"' 

Two parameters, with double quotes intact.

Oh.. what the. I could’ve sworn I could do cd $HOME, but. hm. Okay, is there a way to include » inside the variable? I will try that now.

The reason I’m submitting this answer is you’ll find that StackOverflow is being used by every day users (not just web devs, programmers or power users) and this was the number one result for a simple Windows user question on Google.

People are becoming more tech-savvy, but aren’t necessarily familiar with command line in the cases above.

Читайте также:  Memory allocation in linux

To change to a directory with spaces on the name you just have to type like this:

Hit enter and you will be good

$ DOCS="/cygdrive/c/Users/my\ dir/Documents" 

Here’s your first problem. This puts an actual backslash character into $DOCS , as you can see by running this command:

$ echo "$DOCS" /cygdrive/c/Users/my\ ` 

When defining DOCS , you do need to escape the space character. You can quote the string (using either single or double quotes) or you can escape just the space character with a backslash. You can’t do both. (On most Unix-like systems, you can have a backslash in a file or directory name, though it’s not a good idea. On Cygwin or Windows, \ is a directory delimiter. But I’m going to assume the actual name of the directory is my dir , not my\ dir .)

This passes two arguments to cd . The first is cygdrive/c/Users/my\ , and the second is dir/Documents . It happens that cd quietly ignores all but its first argument, which explains the error message:

-bash: cd: /cygdrive/c/Users/my\: No such file or directory 

To set $DOCS to the name of your Documents directory, do any one of these:

$ DOCS="/cygdrive/c/Users/my dir/Documents" $ DOCS='/cygdrive/c/Users/my dir/Documents' $ DOCS=/cygdrive/c/Users/my\ dir/Documents 

Once you’ve done that, to change to your Documents directory, enclose the variable reference in double quotes (that’s a good idea for any variable reference in bash, unless you’re sure the value doesn’t have any funny characters):

You might also consider giving that directory a name without any spaces in it — though that can be hard to do in general on Windows.

Источник

How to use spaces in a bash alias name?

I am trying to create an aliases in bash. What I want to do is map ls -la to ls -la | more In my .bashrc file this is what I attempted: alias ‘ls -la’=’ls -la | more’ However it does not work because (I assume) it has spaces in the alias name. Is there a work around for this?

4 Answers 4

The Bash documentation states «For almost every purpose, shell functions are preferred over aliases.» Here is a shell function that replaces ls and causes output to be piped to more if the argument consists of (only) -la .

Automatically pipe output:

@sixtyfootersdude: The double-bracket form is more powerful and I use it by habit. See mywiki.wooledge.org/BashFAQ/031

@Jeef: No, my answer uses a function instead of an alias. I have edited it to try to make it clearer.

The first word of each simple command, if unquoted, is checked to see if it has an alias. If so, that word is replaced by the text of the alias. The alias name and the replacement text may contain any valid shell input, including shell metacharacters, with the exception that the alias name may not contain `=’.

So, only the first word is checked for alias matches which makes multi-word aliases impossible. You may be able to write a shell script which checks the arguments and calls your command if they match and otherwise just calls the normal ls (See @Dennis Williamson’s answer)

Читайте также:  Подключение локальной сети линукс

This was helpful because instead of trying to solve it it answered the question. I came here because i wanted to create an alias with a space in it and that just won’t happen.

This not only answered my question, but gave me valuable insight into how the aliasing mechanism actually works. Your quote from the man page was quite helpful.

Источник

Whitespace in command alias

For example, I want to make a command good night and this would look something like alias good night=»many many many commands here» . I tried this but, whitespace is not allowed. Is there any way I could accomplish this?

Try to use good_night, or assign an alias to good, and an alias to night, and see what happens. I think it could work.

You could make a script or Bash function that is named good , which reads its arguments, requires the first one to be «night» and then executes the commands you want.

1 Answer 1

This function should get you started:

Save it as, for example, good.sh , then source it:

good night now will execute various commands (replace the echo statements with whatever you want).

You can add the line that sources your script to the end of your ~/.bashrc file so that the function will be available in every Bash session.

You must log in to answer this question.

Linked

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.14.43533

Ubuntu and the circle of friends logo are trade marks of Canonical Limited and are used under licence.

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

How can I create an alias for a git [action] command (which includes spaces)?

Most of my my aliases are of this form: alias p=’pwd’ I want to alias git commit so that it does git commit -v But trying to create an alias with a space gives an error:

$ alias 'git commit'='git commit -v' -bash: alias: `git commit': invalid alias name 

6 Answers 6

Not a direct answer to your question (since aliases can only be one word), but you should be using git-config instead:

git config --global alias.civ commit -v 

This creates a git alias so that git civ runs git commit -v . Unfortunately, AFAIK there is no way to override existing git commands with aliases. However, you can always pick a suitable alias name to live with as an alternative.

As this answer to the thread that you linked makes clear, it is possible to override existing git commands with aliases, with just a little bit of shell script. (I’m not posting this comment as a criticism, BTW. That answer was posted nearly a year after yours! I’m just posting it in the hope that people might find it helpful.)

You’re talking about a command that includes a space, but here the command is git and there’s no space in there.

Читайте также:  Mysql installation on linux

To call a git commit command, you’d need to write it

git\ commit . 'git commit' . "git commit" . 

Generally commands don’t have space in their names for that reason that it is cumbersome to call them in a shell, so I don’t think you’ll find such a command on your system.

csh, tcsh or zsh will allow you to alias any of those above, but not bash or ksh (though pdksh will allow you but you won’t let you use them). In zsh:

alias "'git commit'=git commit -v" 'git commit' . 

Will make the git command command (when called as ‘git command’ (with the single quotes) only) an alias for the git command with the commit and -v arguments. Not what you were looking for I’d guess though.

Because alias can only alias commands, all you can alias here is the git command, and you’d need to alias it to something that inserts a «-v» after «commit» in its list of arguments. Best would be to go with @jw013’s solution but if for some reason you can’t or wouldn’t, instead of using an alias, you could use a function to do the job:

Источник

fish: whitespace in alias

Now, this is not expanded the way I want (it thinks /home/ben/test is the executable). In bash you can add extra quotes:

alias myfile="'/home/ben/test case/myfile'" 

fish’s alias is only a naive function for familiarity that creates functions with this name (and through eval !), and it’s not surprising to find it buggy. github.com/fish-shell/fish-shell/blob/master/share/functions/… I think you’d better learn how to write fish functions, although that is neither friendly nor quite suitable for interactive fun.

3 Answers 3

alias in fish is just a wrapper for function builtin, it existed for backward compatible with POSIX shell. alias in fish didn’t work as POSIX alias.

If you want the equivalent of POSIX alias , you must use abbr, which was added in fish 2.2.0:

abbr -a myfile "'/home/ben/test case/myfile'" 
abbr -a myfile "/home/ben/test\ case/myfile" 

This answer is migrated from the comment area.

fish’s alias is only a naive function for familiarity that creates functions with this name (and through eval ), and it’s not surprising to find it buggy. I think you’d better learn how to write fish functions.

For your example given, it is converted this line of eval :

function myfile --wraps /home/ben/test; /home/ben/test case/myfile $argv; end 

Therefore using alias myfile=»‘/home/ben/test case/myfile'» won’t help too, as it gives some stupid output like —wraps ‘home/ben/test; ‘/home/ben/test case/myfile’ (look at that lonely single quote!)

So write a function yourself, and you will find fish is such nonsense that is ruins the interactive fun and the friendliness:

function myfile --wraps '/home/ben/test case/myfile'; '/home/ben/test case/myfile' $argv; end 

Note that I haven’t really tested this, and it only works if I got the idea of the syntax right.

And actually I have got enough of the f. -id??tic-shell in 40 minutes when I read through it’s built-in functions the first time.

Источник

Оцените статью
Adblock
detector