-1

A minimal example of my problem is

$ dir='$HOME'
$ cd "$dir" # Not what I want
bash: cd: $HOME: No such file or directory
$ cd $HOME # Works

This is used in another script, where $HOME is output from sed. So I cannot just change dir to "$HOME"

muppi090909
  • 93
  • 1
  • 8
  • 1
    Does this answer your question? [Difference between single and double quotes in Bash](https://stackoverflow.com/questions/6697753/difference-between-single-and-double-quotes-in-bash) – John Kugelman Feb 26 '23 at 06:12
  • The question is more about how to convert from single quotes to double quoted string. – muppi090909 Feb 26 '23 at 06:27
  • 1
    Use `cd "$HOME"` or `cd ~` or `cd`. – Cyrus Feb 26 '23 at 06:34
  • Please see the note in the end. – muppi090909 Feb 26 '23 at 06:39
  • how about something like this: https://stackoverflow.com/questions/42001148/how-to-replace-single-quotes-to-double-quotes-in-bash – Shaun Ramsey Feb 26 '23 at 06:48
  • I think that what I need is a way to expand the string. – muppi090909 Feb 26 '23 at 07:06
  • 2
    Building commands in strings is inherently problematic, see [BashFAQ #50: "I'm trying to put a command in a variable, but the complex cases always fail!"](http://mywiki.wooledge.org/BashFAQ/050) You'd be better off refactoring this to build the command in an array, or just store each piece of data in a separate variable and just use them all as parameters (each in double-quotes) to a regular non-stored command. – Gordon Davisson Feb 26 '23 at 08:36
  • The fundamental problem (revealed in the now-deleted code) was replacing `~` with the literal string `$HOME` in the `dir` variable. The problem goes away if you replace `~` with the value of `$HOME`. It's tricky to do that completely safely with `sed`, but one safe way to do it is: `dir=${dir/#~/"$HOME"}`. – pjh Feb 27 '23 at 14:27

1 Answers1

0

I got it working by using eval. I have to use

$ eval cd '$HOME'
muppi090909
  • 93
  • 1
  • 8
  • Noooo! As cyrus mentioned 18 hrs ago, Use `cd "$HOME"` or `cd ~` or `cd`. Continue to use `eval` to solve these sort of problem and ".... You'll Be SORRY!!!!" . Good luck? – shellter Feb 27 '23 at 00:44
  • It is in a variable. So what I need is a way to expand a string. Any way to do that? – muppi090909 Feb 27 '23 at 04:55
  • In your code, I see `cd $dir`. Just try `cd "$dir"`. Doesn't that work? If not then read https://stackoverflow.com/tags/bash/info . Skip the Version information at the top and search for the sections labeled "Before asking about Problematic code" and "How to turn a bad script into a good question". Then edit your question to show a mimimal version of what is failing you. (Maybe put it at the top in a TLDR section ). Add a comment that you have made this change to the question and commenters will look at it. Good luck. – shellter Feb 27 '23 at 05:03
  • Another bit of advice. Recall that variables can hold what ever data type is appropriate for the surrounding coding eco-system. In this case, the coding system is `bash`. In bash, all variables are essentially strings. (You can store a number in a string) (Yes, you can declare variables to only hold number values. That is more advanced and doesn't seem to be an issue for you). You declare a variable with a string value, `var="myString". ..... – shellter Feb 27 '23 at 05:08
  • To "use" the variable, you add it to a command that is being executed. `ls -l "$var"`. To see what the value IS, you only need to print it, `echo "The value of my \$var=$var"`. Try to use correct terminology in your questions. (-; Good luck! – shellter Feb 27 '23 at 05:09
  • See [Why should eval be avoided in Bash, and what should I use instead?](https://stackoverflow.com/q/17529220/4154375). – pjh Feb 27 '23 at 14:20