tilde expansion in environment variable
use
SOME_PATH=~/path/to/path/
if you path have spaces, quote it
SOME_PATH=~/"path with spaces"
tilde expansion when evaluating $PATH
No, this is not portable. If your operating system's execl
call doesn't perform this expansion, programs that aren't bash will fail to find executables within the given directory.
Note that if you didn't quote the expansion, this would be safe:
PATH=$PATH:~/tempdir # this is safe -- expansion happens before assignment
PATH="$PATH:~/tempdir" # this is not -- readers are required to expand
While it's generally a best practice to quote all shell expansions, this isn't necessary for assignments, which implicitly prevent string-splitting and glob expansion behaviors.
Tilde not expanded when quoting on the right hand side of a Bash variable assignment
Quotation marks prevent expansion of ~
. Replace ~
with $HOME
or use dirName=~/"test_$x"
.
From the manual's explanation of tilde expansion:
Each variable assignment is checked for unquoted tilde-prefixes immediately following a : or the first =. In these cases, tilde expansion is
also performed.
Can I use environment variables or tilde in module.modulemap?
No, the modulemap syntax does not expand tildes or environment variables. It ultimately just expects to stat
the path you gave it, and if no file's there, it'll gripe.
- Here's where the header file lookup is kicked off, during lexing of the module map file.
- It ultimately passes the path to the SourceManager's FileManager to produce a
File
object, as here for a header in a framework'sHeaders/
public header folder. getFile
ultimately ends up calling out togetStatValue
, which does a cache lookup.- The
FileSystemStatCache::get
eventually grounds out in LLVM's filesystem abstraction, where it callssys::fs::status
, which is documented to act like POSIXstat
. - POSIX
stat
works with paths as-is, no tilde or environment variable expansion - the common availability of those is due to the shell helping you out, not something that happens automatically most of the time at the system level.
However, it's standard to use relative paths in module maps. The lexer respects this, and all the module map docs demonstrate this. In the common case where your module map file is colocated with your library and installed alongside it, this should suffice to properly resolve the paths.
How to manually expand a special variable (ex: ~ tilde) in bash
Due to the nature of StackOverflow, I can't just make this answer unaccepted, but in the intervening 5 years since I posted this there have been far better answers than my admittedly rudimentary and pretty bad answer (I was young, don't kill me).
The other solutions in this thread are safer and better solutions. Preferably, I'd go with either of these two:
- Charle's Duffy's solution
- Håkon Hægland's solution
Original answer for historic purposes (but please don't use this)
If I'm not mistaken, "~"
will not be expanded by a bash script in that manner because it is treated as a literal string "~"
. You can force expansion via eval
like this.
#!/bin/bash
homedir=~
eval homedir=$homedir
echo $homedir # prints home path
Alternatively, just use ${HOME}
if you want the user's home directory.
Why use $HOME over ~ (tilde) in a shell script?
Tilde expansion doesn't work in some situations, like in the middle of strings like /foo/bar:~/baz
Tilde (~/) not working on if then statement in Shell script
Double quotes (really, any quotes) prevent ~
from being expanded. Use instead:
checkfile=~/mysql_backup/"$file"
...or, even better, use $HOME
in all scripts, and consider ~
to be a facility for interactive/human use only.
How to expand tilde (~) in path
Original answer
Try this:
eval directory="$directory"
Since nothing can interpret the special shell characters better than the shell itself, it is a good idea to ask the shell to evaluate the expression for us. And eval
is just the command that evaluates shell expressions.
Alternative #1: program in C
However, eval
is unsafe, as it has been mentioned many times, - it may execute malicious code, or cause unwanted effects. Then, for a POSIX environment, you can write a simple program in C:
tildeexp.c
#include <stdio.h>
#include <stdlib.h>
#include <wordexp.h>
int
main(int argc, char **argv)
{
wordexp_t p;
int rc;
rc = wordexp(argv[1], &p, 0);
if (rc) {
fprintf(stderr, "Failed to expand %s: %d\n",
argv[1], rc);
} else {
printf("%s\n", p.we_wordc ? p.we_wordv[0] : "");
}
wordfree(&p);
return (rc ? 1 : 0);
}
Compiling
gcc -Wall -g -O2 -o tildeexp tildeexp.c
Usage
directory=$(/path/to/tildeexp "$directory")
if [ $? -eq 0 ]; then
# success
else
# failed to expand
fi
Alternative #2: Perl's glob
directory="${directory//$"'"/$"\\'"}"
directory=$(perl -e "print glob('$directory')")
Related Topics
How to Add Deploy Key for 2 Repo with 1 User on Github
Why Does Arm-Linux-Gnueabi-G++-4.4 Always Build a "7-A" Binary
Why Is Dd with the 'Direct' (O_Direct) Flag So Dramatically Faster
Node Server Crashes After Few Hours
What Does Double-Dash Do When Following a Command
Why Does %Rbp Point to Nothing
Why Crontab Uses or When Both Day of Month and Day of Week Specified
How to Insert New Line in the Email Using Linux Mail Command
Link a Static Library to a Shared One During Build
Setting Limit to Total Physical Memory Available in Linux
Android Studio 2.3 Using Emulator from Console, "/Dev/Kvm Device: Permission Denied" for Root User
Define Alias That References Other Aliases
Identify Other End of a Unix Domain Socket Connection
Weird Sigsegv Segmentation Fault in Std::String::Assign() Method from Libstdc++.So.6