What is tilde (~) in Mac/Linux?

The tilde(~) expansion in Mac/Linux has several pathnames.

  • It could be ${HOME} if you do not specify the username.
    If you specify the user, it will expend the home directory of the user.
    For example,

    $ echo ~
    $ echo ~otheruser
  • It contains the working directory by +/-
    For example,

    $ echo ~+
    $ echo ~-

In shell, if you want to expand it,

EXPAND_PATH=`eval echo ~otheruse/bin/`

bash shell에서 eval, exec, source(.)에 관한 노트

Bash Shell 초보자가 헥깔리기 쉬운 eval, exec, source(.)에 관련한 노트입니다.

원문은 bash shell: ‘exec’, ‘eval’, ‘source’ – looking for help to understand 에서 발견했습니다. 여기에 기록상 카피합니다.

1. eval “translates” a value buried inside a variable, and then runs the command that was buried in there

for i in 1 2 3
 eval myvar="$i"
 echo "$myvar"
# this gives
# why? because there is no metavalue or special meaning to 1 or 2 or 3
for i in ls df
 eval myvar="$i"
 echo "$myvar"
# here you get output from the ls command and the df command

2. exec starts another process – BUT – it exits the current process when you do this kind of thing


exec echo "leaving this script forever  $0"   # Exit from script here.

# ----------------------------------
# The following line never happens

echo "This echo will never echo."

3. source

When you run a command in the shell – like another script or a command like ls –
the shell creates a subprocess (called child process). Any environment variable that got defined or changed down in the child is LOST FOREVER to the parent process.

However if you source a script (there are two ways) you force the script to run in the current process. That means environment variables in the script you ran are NOT LOST.

# script1
MYVAR="HI there!"
# end script1

# script2
# first, run script 1
# now is the MYVAR variable set?  No -why? it got set in the child, when the child exited it GOT LOST
echo "MYVAR= $MYVAR"
# now source script1 :: putting a lonesome dot or the command  "source"
#  in front of the script name have the same effect
. script1
#  . script1 == source script1  these two are the SAME THING
source script1
# now is the MYVAR variable set?  This time we see that MYVAR has a value
echo "MYVAR= $MYVAR"

xargs로 입력받은 argument들을 반복해서 처리해야 할때,

for x in $@
  tar -cvzf $x".tgz" $x"/"

위와 같은 shell script를 만들어둡니다. 위의 예제는 디렉토리 이름이 argument로 넘어올때, tar 압축을 해주게 합니다. 위의 shell script의 이름이 dmake_tar.sh 라고 하면, 다음 문장을 실행하면, 각 디렉토리 이름별로 압축이 됩니다.

find -maxdepth 1 -type d -regex "./.*" -printf "%f\n" | xargs ./dmake_tar.sh