[Share Experiences] ~/Desktop和/home/$USER/Desktop有什么不同
Tofloor
poster avatar
酷谷的谷子
deepin
2022-01-29 17:01
Author

Desktop_dir_1="~/Desktop"

Desktop_dir_2="/home/$USER/Desktop"

$Desktop_dir_1 这样不能用,但是直接使用~/Desktop却又可以很疑惑

$Desktop_dir_2 这样可以用

Reply Favorite View the author
All Replies
andktan
deepin
2022-01-29 17:11
#1

1、当前用户的桌面

2、用户名为user的桌面

3、我这里用着没有问题

4、我这里没有user这个用户,显示都是空的

Reply View the author
Sun
deepin
2022-01-29 17:34
#2
andktan

1、当前用户的桌面

2、用户名为user的桌面

3、我这里用着没有问题

4、我这里没有user这个用户,显示都是空的

并不是 User用户 $User 取得是当前用户.

Reply View the author
Sun
deepin
2022-01-29 17:37
#3

$USER 是个变量

/home/$USER/Desktop 这个语句我理解在你使用时候,系统没有把变量解析出来,导致无法进入你的桌面。

Reply View the author
andktan
deepin
2022-01-29 17:40
#4
Sun

并不是 User用户 $User 取得是当前用户.

blush

Reply View the author
beavailable
deepin
2022-01-29 18:55
#5

~ 就表示 $HOME 也就是 /home/$USER

在双引号里面,可以用 $ 来引用变量,但不能用 ~

Reply View the author
Hibanaw
deepin
2022-01-29 19:16
#6

据我所知,~只能在终端命令行里用,sh脚本里不行

Reply View the author
酷谷的谷子
deepin
2022-01-29 19:20
#7
beavailable

~ 就表示 $HOME 也就是 /home/$USER

在双引号里面,可以用 $ 来引用变量,但不能用 ~

嗯,在弄脚本的时候绝的奇怪,就发上来

深度截图_选择区域_20220129110233.png

Reply View the author
waiting
deepin
2022-01-29 21:12
#8

大致这么理解吧:

  1. $HOME 是个变量,可以通过(双引号)进行赋值,赋值结果是解析出来的实际路径
  2. ~ 是个指向用户主目录的(软?)链接,在赋值的时候如果在引号之内是不会被解析展开的,但可以通过路径赋值
    1. dir_1="~/Destkop" , 变量 dir_1 的结果是字符串
    2. dir_2=~/Desktop ,变量 dir_2 的值是展开的路径 /home/$USER/Desktop , 可以执行命令 cd $dir_2
Reply View the author
waiting
deepin
2022-01-29 23:10
#9
#!/bin/bash

dir=~/Desktop

echo $dir
cd $dir
pwd

输出

/home/mark/Desktop
/home/mark/Desktop

Reply View the author