Что делает маленький волнистый ~ в Linux?

23824
O_O

У меня есть два случая его использования, и мне интересно, что каждый из них делает:

  1. service=~

  2. mv ~/Desktop/Service$version.tgz $service

Что делает маленький волнистый ~?

Затем, после этого, что бы cd $serviceсделать?

25
это так заслуживает тега * [squiggly] * cregox 13 лет назад 3
Тильда, это специфическая оболочка, а не специфичная для Linux. David Allan Finch 13 лет назад 3
@Cawas: Спроси, и ты получишь. Dave Sherohman 13 лет назад 0
@ Дэйв спасибо! Но кажется, что Крис ненавидел это. Ну что ж, жизнь продолжается ... cregox 13 лет назад 0
Это действительно первый раз, когда об этом спрашивают? Я ожидал, что это будет закрыто как дубликат. Erik B 13 лет назад 0

5 ответов на вопрос

44
Mikel

The squiggly thing is called a "tilde".

It expands to your home directory.

Try

echo ~ echo $HOME 

Both statements put your home directory by itself on a line..

See bash Tilde Expansion for details.

Приведенное выше сравнение, хотя и допустимо, возможно, вводит в заблуждение: тильда работает только во время интерпретации оболочки в сценариях и в командной строке. Однако переменная окружения $ HOME работает везде, где работает переменная окружения, а это гораздо больше мест. Mei 13 лет назад 8
36
Wuffers

The "squiggly" is called a tilde. It is used to refer to your home directory which on Linux, is normally /home/username. It is also stored in the $HOME environment variable. Expanding the ~ to the location of the home directory is the job of the shell (like zsh or bash) or file manager (like Nautilus) and not the filesystem or OS its self.

You can also use this to refer to another user's home directory. For instance, if the other user's username is bob, you could refer to their home directory with ~bob, which will be expanded to /home/bob/.

The first example you've given sets the variable service to ~, so it corresponds to your home directory. This is equivalent to service=/home/username or service=$HOME.

The second example copies the file ~/Desktop/Service$version.tgz (or /home/username/Desktop/Service$version.tgz) to /home/username. This command is equivalent to:

mv ~/Desktop/Service$version.tgz ~ 

or

mv ~/Desktop/Service$version.tgz $HOME 

or

mv ~/Desktop/Service$version.tgz /home/username/ 

The third will change the current working directory ($PWD) to /home/username/. This is equivalent to:

cd /home/username/ 

or

cd $HOME 
Следует помнить еще одну вещь: расширение Tilde - это работа оболочки или файлового менеджера, это не функция самой файловой системы Linux. Таким образом, он часто не работает в конфигурационных файлах, а добавление кавычек вокруг "~" останавливает его расширение в оболочке. Grumbel 13 лет назад 3
Также следует отметить, что домашние каталоги не обязательно лежат в / home, поэтому не следует предполагать, что ~ расширяется до / home / [мое имя пользователя] или что ~ bob расширяется до / home / bob darkliquid 13 лет назад 3
Спасибо за предложение @Grumbel. И спасибо @PriceChild за добавление предложения @ darkliquid! Wuffers 13 лет назад 0
10
nomaderWhat

In both #1 & #2: ~ is your home directory, so if you are qwerty it will likely be the directory /home/qwerty. So try ls ~ to see that.

For #1: it looks to me like the variable service is being defined as your home directory.

That means after #2 has moved the tgz file from the Desktop subdirectory to your home directory, #3 then changes to the home directory.

5
James

It looks like the commands are doing the following.

  1. Assign a variable called service to your home folder location, for example:

    /home/user 
  2. It moves the file from your desktop to the top level of your home directory, for example:

    /home/user/Desktop/Service$version.tgz $service 
  3. The script then changes the directory to the top level of the home directory.

So, all the script is doing is just cleaning up your desktop by moving the file to your /home/user folder instead.

1
Rooke

Здесь я добавлю, что ~ user также раскрывает домашний каталог [user], так что это не просто ярлык для вашего собственного домашнего каталога.

Например:

[guy@abox anotherdir]$ cd ~  [guy@abox ~]$ pwd /home/guy [guy@abox ~]$ cd ~john [guy@abox john]$ pwd /home/john 

Похожие вопросы