Answer by xicod for How to manually expand a special variable (ex: ~ tilde)...
For anyone's reference, a function to mimic python's os.path.expanduser() behavior (no eval usage):# _expand_homedir_tilde ~/.vim/root/.vim# _expand_homedir_tilde ~myuser/.vim/home/myuser/.vim#...
View ArticleAnswer by alexis for How to manually expand a special variable (ex: ~ tilde)...
I think thatthepath=( ~/abc/def/ghi )is easier than all the other solutions... or I am missing something? It works even if the path does not really exists.
View ArticleAnswer by Yordan Georgiev for How to manually expand a special variable (ex:...
for some reason when the string is already quoted only perl saves the day #val="${val/#\~/$HOME}" # for some reason does not work !! val=$(echo $val|perl -ne 's|~|'$HOME'|g;print')
View ArticleAnswer by glibbond for How to manually expand a special variable (ex: ~...
Here is a ridiculous solution:$ echo "echo $var" | bashAn explanation of what this command does:create a new instance of bash, by... calling bash;take the string "echo $var" and substitute $var with...
View ArticleAnswer by Paul M for How to manually expand a special variable (ex: ~ tilde)...
why not delve straight into getting the user's home directory with getent?$ getent passwd mike | cut -d: -f6/users/mike
View ArticleAnswer by JamesIsIn for How to manually expand a special variable (ex: ~...
I have done this with variable parameter substitution after reading in the path using read -e (among others). So the user can tab-complete the path, and if the user enters a ~ path it gets sorted. read...
View ArticleAnswer by Karim Alibhai for How to manually expand a special variable (ex: ~...
Simplest: replace 'magic' with 'eval echo'.$ eval echo "~"/whatever/the/f/the/home/directory/isProblem: You're going to run into issues with other variables because eval is evil. For instance:$ # home...
View ArticleAnswer by go2null for How to manually expand a special variable (ex: ~ tilde)...
Here is the POSIX function equivalent of Håkon Hægland's Bash answerexpand_tilde() { tilde_less="${1#\~/}" [ "$1" != "$tilde_less" ] && tilde_less="$HOME/$tilde_less" printf...
View ArticleAnswer by mikeserv for How to manually expand a special variable (ex: ~...
Just use eval correctly: with validation.case $1${1%%/*} in([!~]*|"$1"?*[!-+_.[:alnum:]]*|"") ! :;;(*/*) set "${1%%/*}""${1#*/}" ;;(*) set "$1"esac&& eval "printf '%s\n' $1${2+/\"\$2\"}"
View ArticleAnswer by Chris Johnson for How to manually expand a special variable (ex: ~...
You might find this easier to do in python.(1) From the unix command line:python -c 'import os; import sys; print os.path.expanduser(sys.argv[1])' ~/fredResults in:/Users/someone/fred(2) Within a bash...
View ArticleAnswer by Gino for How to manually expand a special variable (ex: ~ tilde) in...
Here's my solution:#!/bin/bashexpandTilde(){ local tilde_re='^(~[A-Za-z0-9_.-]*)(.*)' local path="$*" local pathSuffix= if [[ $path =~ $tilde_re ]] then # only use eval on the ~username portion !...
View ArticleAnswer by Orwellophile for How to manually expand a special variable (ex: ~...
I believe this is what you're looking for magic() { # returns unexpanded tilde express on invalid user local _safe_path; printf -v _safe_path "%q""$1" eval "ln -sf ${_safe_path#\\} /tmp/realpath.$$"...
View ArticleAnswer by Charles Duffy for How to manually expand a special variable (ex: ~...
Plagarizing myself from a prior answer, to do this robustly without the security risks associated with eval:expandPath() { local path local -a pathElements resultPathElements IFS=':' read -r -a...
View ArticleAnswer by Håkon Hægland for How to manually expand a special variable (ex: ~...
If the variable var is input by the user, eval should not be used to expand the tilde usingeval var=$var # Do not use this!The reason is: the user could by accident (or by purpose) type for example...
View ArticleAnswer by eddygeek for How to manually expand a special variable (ex: ~...
A safe way to use eval is "$(printf "~/%q""$dangerous_path")". Note that is bash specific.#!/bin/bashrelativepath=a/b/ceval homedir="$(printf "~/%q""$relativepath")"echo $homedir # prints home pathSee...
View ArticleAnswer by Noach Magedman for How to manually expand a special variable (ex: ~...
Expanding (no pun intended) on birryree's and halloleo's answers: The general approach is to use eval, but it comes with some important caveats, namely spaces and output redirection (>) in the...
View ArticleAnswer by halloleo for How to manually expand a special variable (ex: ~...
Just to extend birryree's answer for paths with spaces: You cannot use the eval command as is because it seperates evaluation by spaces. One solution is to replace spaces temporarily for the eval...
View ArticleAnswer by Jay for How to manually expand a special variable (ex: ~ tilde) in...
How about this:path=`realpath "$1"`Or:path=`readlink -f "$1"`
View ArticleAnswer by wkl for How to manually expand a special variable (ex: ~ tilde) in...
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...
View ArticleHow to manually expand a special variable (ex: ~ tilde) in bash
I have a variable in my bash script whose value is something like this:~/a/b/cNote that it is unexpanded tilde. When I do ls -lt on this variable (call it $VAR), I get no such directory. I want to let...
View Article
More Pages to Explore .....