cd to a folder name starting with tilde (~) in bash script

此生再无相见时 提交于 2021-02-16 21:15:29

问题


I'm trying to create a very simple bash script that does the following:

  • Inputs the user to enter a project folder
  • Sets the value entered in a variable
  • cd to the folder entered
  • remove the readme.md file

From what I've read around I've come up with this:

#!/bin/bash

echo "Enter the project folder path:"
read -e DESTINATION_FOLDER

cd "$DESTINATION_FOLDER"
rm -rf "$DESTINATION_FOLDER/readme.md"

The folder's path I entered was

~/Dropbox/myproject

And the error I get is the one below. However that folder exists and I can access it manually using the cd command.

./cleanup.sh: line 6: cd: ~/Dropbox/myproject/: No such file or directory

Can anyone please point out what I'm doing wrong?


回答1:


The tilde (~) is only expanded to the home directory if it appears directly (and is not quoted). When you use cd "$DESTINATION_FOLDER", the characters in $DESTINATION_FOLDER are used literally, including the leading ~. Even if you had used cd $DESTINATION_FOLDER, the ~ would have been inserted directly, although whitespace and glob metacharacters in $DESTINATION_FOLDER would have been expanded. So there is really no way to do tilde expansion on a value in a variable. [Note 1]

Some people will suggest using eval, but you have to be very careful about that. eval cd "$DESTINATION_FOLDER" could have unexpected results if $DESTINATION_FOLDER includes a shell metacharacter. Consider the following, for example (don't try this in a directory which contains files you care about):

$ mkdir -p ~/tmp/foo && cd ~/tmp/foo
$ touch a few files
$ ls
a  few  files
$ x="~/tmp/foo;rm *"
$ # BEWARE!
$ eval cd "$x"
$ # OOPS!
$ ls
$

The correct way to do this is to expand the tilde yourself:

cd "${DESTINATION_FOLDER/#~/$HOME}"

In bash, ${var/pattern/replacement} produces a string based on the value of $var, where the first instance of pattern (if any) is replaced with replacement. The form ${name/#pattern/replacement} only does the replacement if pattern occurs at the beginning of $var. pattern is a glob, not a regular expression.

EDIT: In a comment, Tom Fenech suggested that it would be cool if it were possible to handle ~user as well as just ~. In the same vein, it would be even cooler to handle bashisms like ~+2 [Note 2].

Indeed. Unfortunately, bash does not provide a builtin which does tilde expansion. Or any other kind of expansion. So I think it is necessary to use eval, but that requires considerable caution, as mentioned above. I think that the best that can be done is to extract the tilde-prefix only if it contains no metacharacters, and only then expand it with eval printf %s, something like this:

tilde-expand() {
  (
    shopt -s extglob
    if [[ $1 =~ ^~[[:alnum:]_.+-]*(/.*)?$ ]]; then
      printf %s "$(eval printf ${1%%/*})${1##*([^/])}"
    else
      printf %s "$1"
    fi
  )
}

(The above hasn't been carefully tested. As always, eval should be treated with the greatest of caution, so please examine carefully before deploying.)


Notes:

  1. In general, this doesn't matter, because most scripts take input as arguments rather than reading them from the console. Such a script might be written as follows:

    cdrm() {
      cd "$1" && rm -rf readme.md
    }
    

    then you would be able to invoke it using a tilde:

    cdrm ~/Dropbox/myproject/
    

    because the ~ will be expanded in the call to cdrm, so that $1 will have the expanded value.

  2. I've never used ~+N tilde expansions, but there are probably people who find them essential.




回答2:


It's the shell that expands a ~ into your home directory when you're typing things into an interactive shell. It won't work from a script.

You'd need to type the full path in to get it to work. (It can be a relative path, of course.)

See this SO question for how to expand a tilde in a bash script.



来源:https://stackoverflow.com/questions/25594495/cd-to-a-folder-name-starting-with-tilde-in-bash-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!