Quantcast
Channel: How to manually expand a special variable (ex: ~ tilde) in bash - Stack Overflow
Browsing all 20 articles
Browse latest View live

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 Article



Answer 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 Article

Answer 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 Article

Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article

Answer 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 Article


Answer 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 Article


Answer 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 Article

Answer 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 Article

Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article

How 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
Browsing all 20 articles
Browse latest View live




Latest Images