Linux 符号链接中的环境变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3888809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Environment variables in symbolic links
提问by kfl62
Can we use $HOME or other environment variable in symbolic links?
我们可以在符号链接中使用 $HOME 或其他环境变量吗?
I know about using relative paths ../../.config
but sometimes are to many ../
:) something like ~/.config
would be more comfortable, or use of $HOME.
我知道使用相对路径,../../.config
但有时是很多../
:) 类似的东西~/.config
会更舒服,或者使用 $HOME。
Edit:
编辑:
habbie's answer with psmears's comment is the answer, sorry my question was incomplete.
哈比对psmears评论的回答就是答案,抱歉我的问题不完整。
While (as other answers show) you can use environment variables when creating symbolic links (as with any shell command!), you can't actually have environment variable (or '~') references in the symlink itself
虽然(如其他答案所示)您可以在创建符号链接时使用环境变量(与任何 shell 命令一样!),但您实际上不能在符号链接本身中使用环境变量(或“~”)引用
采纳答案by Habbie
Symbolic links are handled by the kernel, and the kernel does not care about environment variables. So, no.
符号链接由内核处理,内核不关心环境变量。所以不行。
回答by thomasmalt
yes. no problem. actually you won't actually be using the $HOME variable in your link, so it won't work with smart solutions for groups of users for example. The variable is translated by the shell when executing the command, and the content of the variable is used in the link.
是的。没问题。实际上,您实际上不会在链接中使用 $HOME 变量,因此它不适用于例如用户组的智能解决方案。该变量在执行命令时由shell翻译,在链接中使用该变量的内容。
ln -s ~/test /tmp/test
is expaned to
被扩展为
/<path>/<to>/home/test -> /tmp/test
Ah. and only the environment variables of the person calling ln will work. You can't store other peoples environment variables in the link. The variables are expanded before calling the command.
啊。并且只有调用 ln 的人的环境变量才能工作。您不能在链接中存储其他人的环境变量。在调用命令之前扩展变量。
回答by kogakure
Yes you can.
是的你可以。
ln -s $HOME/file/or/folder newname
You can set your own variables and use them, too. Add in your .bashrc (or .bash_profile):
您也可以设置自己的变量并使用它们。添加您的 .bashrc(或 .bash_profile):
export $MYPATH=/your/path
回答by jazstik
If you don't want to expand the variable in the link you can put single quotes around it,
如果你不想扩展链接中的变量,你可以在它周围加上单引号,
ln -s '$HOME/file/or/folder' newname
This would give,
这会给,
newname -> $HOME/file/or/folder
rather than have it expand to your locally set $HOME. As described in other answers it will not expand it at all. So you can e.g. use it to symlink to a file inside the literal $HOME
folder.
而不是将其扩展到您本地设置的 $HOME。如其他答案中所述,它根本不会扩展它。因此,您可以例如使用它来符号链接到文字$HOME
文件夹内的文件。
[Note this is system dependent - not all systems support variant symlinks]
[请注意,这取决于系统 - 并非所有系统都支持变体符号链接]
回答by Perkins
The closest I've been able to come is using a FUSE
filesystem. It's pretty simple to use fusepy
to write a custom passthrough filesystem, which can read environment variables when determining what real file to give. Of course, it only gets the environment variables of the process which mounted the passthrough system, so it's not as useful as it could be.
我最接近的是使用FUSE
文件系统。fusepy
编写自定义直通文件系统非常简单,它可以在确定要提供的真实文件时读取环境变量。当然,它只获取挂载透传系统的进程的环境变量,所以它不是那么有用。
回答by Ronald Hoogenboom
Even though the symbolic links are resolved by the kernel, you could still do a LD_PRELOAD trick, wrapping all libc functions that take pathnames and expand any $XYZ components in the string returned by 'readlink' (parameter expansion). Then feed the expanded path to the wrapped function. You have to escape the target path from shell expansion when creating the link, as jaztik suggests.
即使符号链接由内核解析,您仍然可以执行 LD_PRELOAD 技巧,包装所有采用路径名的 libc 函数,并在“readlink”(参数扩展)返回的字符串中扩展任何 $XYZ 组件。然后将扩展路径提供给包装函数。正如 jaztik 所建议的那样,在创建链接时,您必须从 shell 扩展中转义目标路径。
As the injected library has full access to the users' environment, this will fulfill all expectations of the OP.
由于注入的库可以完全访问用户的环境,因此这将满足 OP 的所有期望。