bash script dollar sign operator $ problem - is it incompatible with ~ operand?


Tfb9

I am new to Linux environment. After a quick search, I couldn't find a useful question about the $ operator used in bash scripts. I don't know if this question is related to the Ubuntu (or other) flavor of Linux, or my programming experience.

Basically, I think the ~ operand in Linux is exactly equivalent to the string '/home/username'. If I try to execute the program after a certain delay by using a bash script and a C-based external program, I'm stuck: I can't take advantage of the ~ operand and make the program indifferent to the user (though the rest must obey the file path).

Please provide the following comments (finding the getIdle source code is left to the reader):

#!/bin/bash
# call.sh

idle=false
idleAfter=30000     # consider idle after 30000 ms

# EXPLAIN THIS - Begin
# Strings that work
# str_getIdle_exe='/home/seb/Documents/Seb/Prjcts/Idle/getidle/src/getIdle'
# str_getIdle_exe="/home/seb/Documents/Seb/Prjcts/Idle/getidle/src/getIdle"

# Strings that don'work
str_getIdle_exe='~/Documents/Seb/Prjcts/Idle/getidle/src/getIdle'
# str_getIdle_exe="~/Documents/Seb/Prjcts/Idle/getidle/src/getIdle"
# EXPLAIN THIS - End


while true; do
  idleTimeMillis= $str_getIdle_exe
  echo $idleTimeMillis  # just for debug purposes.
  if [[ $idleTimeMillis -gt $idleAfter && $idle = false  ]] ; then
    echo "start idle"   # or whatever command(s) you want to run...
    idle=true
    /usr/bin/xflock4
  else  # just for debug purposes.
    echo "not there yet..."
  fi

  if [[ $idleTimeMillis -lt $idleAfter && $idle = true ]] ; then
    echo "end idle"     # same here.
    idle=false
  fi
  sleep 1      # polling interval

done

What I'm trying to explain is: why do the two different strings above execute fine in the shell, but not when called from a script?

I'm using Linux Mint 17.1 "Rebecca" Xfce (64bit).

thank you very much.

Glen Jackman

A tilde in quotes does not expand -ref : https://www.gnu.org/software/bash/manual/bashref.html#Tilde-Expansion

So you want to remove the quotes:

str_getIdle_exe=~/Documents/Seb/Prjcts/Idle/getidle/src/getIdle

If the path under homedir contains spaces, unquote the tilde:

some_path=~/"dir with spaces/file with spaces"

Related


Bash script to print dollar sign and \r in awk

Tony I have a bash script that helps and then use awk to get some variables - the first works fine, but the second has a leading $ and trailing \r... any ideas? #!/bin/bash newTS=$( cat times.txt | awk -F';' '{print $2}') ctime=$( cat times.txt | awk -F';' '{p

Operand type is incompatible with operator

Noppadet i get error Operand type is incompatible with operator Click OK in the header when trying to compare two "real" data types. Can anyone help me with what problem? public void clicked() { real localAnnualUsage = itemSetup_DS.AnnualUsage();

Operand type is incompatible with operator

Noppadet i get error Operand type is incompatible with operator Click OK in the header when trying to compare two "real" data types. Can anyone help me with what problem? public void clicked() { real localAnnualUsage = itemSetup_DS.AnnualUsage();

Dollar sign interpolation inside quotes in bash

Amir Afghani I'm running this little test script I wrote: #!/bin/bash TESTCASE=$@ testHarness <<runTest $TESTCASE runTest From the command line: ./test.sh "1092$212" But the dollar sign is stripped. If I get away with it, that's fine. But I think double quo

Dollar sign interpolation inside quotes in bash

Amir Afghani I'm running this little test script I wrote: #!/bin/bash TESTCASE=$@ testHarness <<runTest $TESTCASE runTest From the command line: ./test.sh "1092$212" But the dollar sign is stripped. If I get away with it, that's fine. But I think double quo

Does Julia have an operator similar to the Haskell dollar sign?

Eli Sadoff Haskell's dollar sign is a great function, f $ ge.g. for , evaluates fwith the result gof . This works fine sin $ sqrt $ abs $ f 2 equivalent to sin(sqrt(abs(f(2)))) The dollar sign works fine for me because it's more readable. I've noticed |>what

Does Julia have an operator similar to the Haskell dollar sign?

Eli Sadoff Haskell's dollar sign is a great function, f $ ge.g. for , evaluates fwith the result gof . This works fine sin $ sqrt $ abs $ f 2 equivalent to sin(sqrt(abs(f(2)))) The dollar sign works fine for me because it's more readable. I've noticed |>what

Does Julia have an operator similar to the Haskell dollar sign?

Eli Sadoff Haskell's dollar sign is a great function, f $ ge.g. for , evaluates fwith the result gof . This works fine sin $ sqrt $ abs $ f 2 equivalent to sin(sqrt(abs(f(2)))) The dollar sign works fine for me because it's more readable. I've noticed |>what

Does Julia have an operator similar to the Haskell dollar sign?

Eli Sadoff Haskell's dollar sign is a great function, f $ ge.g. for , evaluates fwith the result gof . This works fine sin $ sqrt $ abs $ f 2 equivalent to sin(sqrt(abs(f(2)))) The dollar sign works fine for me because it's more readable. I've noticed |>what

Does Julia have an operator similar to the Haskell dollar sign?

Eli Sadoff Haskell's dollar sign is a great function, f $ ge.g. for , evaluates fwith the result gof . This works fine sin $ sqrt $ abs $ f 2 equivalent to sin(sqrt(abs(f(2)))) The dollar sign works fine for me because it's more readable. I've noticed |>what

Does Julia have an operator similar to the Haskell dollar sign?

Eli Sadoff Haskell's dollar sign is a great function, f $ ge.g. for , evaluates fwith the result gof . This works fine sin $ sqrt $ abs $ f 2 equivalent to sin(sqrt(abs(f(2)))) The dollar sign works fine for me because it's more readable. I've noticed |>what

What are $0 and $1 (dollar sign 0 or 1) in awk script?

0x90: inawk : 1 tolower($1) ~ /mary/ { print "CI Record: " $0; } 2 $0 !~ /Mary/ { print "Not Mary: " $0; } 3 $1 == "Mary" { print "Mary Record: " $0; } Why 1and 3compare $1and 2use $0? Anubava: In fact, the example is using regular expressions # 2becaus

What are $0 and $1 (dollar sign 0 or 1) in awk script?

0x90: inawk : 1 tolower($1) ~ /mary/ { print "CI Record: " $0; } 2 $0 !~ /Mary/ { print "Not Mary: " $0; } 3 $1 == "Mary" { print "Mary Record: " $0; } Why 1and 3compare $1and 2use $0? Anubava: In fact, the example is using regular expressions # 2becaus

What does the double dollar sign in bash/Makefile mean?

John Totugo: When inserting a shell script in a Makefile, we can use (?) double dollar signs ($$) to refer to variables. why? for number in 1 2 3 4 ; do \ echo $$number ; \ done Anubava: According to the official documentation of gnu make : Variable and f