1

1st command

RELAY_MASTER_LOG_FILE=$(mysql -uroot -p1234 -e 'show slave status \G'| grep 'Relay_Master_Log_File' |cut -d: -f2|tr -d " ")
echo $RELAY_MASTER_LOG_FILE

result

node-bin.000002

(no space before the variable value)

but when I try to execute this command

SET_REPLICATION_PARAMS="MASTER_LOG_FILE='$RELAY_MASTER_LOG_FILE'"

I get this result

MASTER_LOG_FILE=' node-bin.000002'

there is a space before the variable value

Renaud Pacalet
  • 25,260
  • 3
  • 34
  • 51
  • Are you definitely sure the first output does not have a space or hidden characters? Write it to a file `echo $VAR_NAME > file.txt` and check the file size. – Aziz Feb 04 '22 at 03:30
  • yes I am sure.. I'll try your advised – Raymund Dalagan Feb 04 '22 at 03:49
  • what will I do after checking the file size? – Raymund Dalagan Feb 04 '22 at 03:53
  • 1
    Please make sure to enclose the variable with double quotes as `echo "$VAR_NAME" > file.txt`. Then please post the result of `xxd file.txt` to see if any odd character is included. – tshiono Feb 04 '22 at 04:40
  • `echo $RELAY_MASTER_LOG_FILE` is not a good way to see what a variable contains. Among other things, it *will* remove spaces at the beginning of the value; see ["I just assigned a variable, but `echo $variable` shows something else"](https://stackoverflow.com/questions/29378566/i-just-assigned-a-variable-but-echo-variable-shows-something-else). Even with double-quotes, it can still be misleading in a number of ways. `declare -p RELAY_MASTER_LOG_FILE` tends to be much clearer. Putting `set -x` before the problematic section to get an execution trace is another good method. – Gordon Davisson Feb 04 '22 at 04:43
  • @tshiono this is the result MASTER_LOG_FILE=' node-bin.000002' – Raymund Dalagan Feb 04 '22 at 05:52
  • What about the result of `xxd file.txt`? `xxd` is a command name. – tshiono Feb 04 '22 at 05:54
  • 00000000: 4d41 5354 4552 5f4c 4f47 5f46 494c 453d MASTER_LOG_FILE= 00000010: 270a 6e6f 6465 2d62 696e 2e30 3030 3030 '.node-bin.00000 00000020: 3227 0a – Raymund Dalagan Feb 04 '22 at 06:21
  • I'm sorry @RenaudPacalet but I don't follow you – Raymund Dalagan Feb 04 '22 at 07:08
  • @RaymundDalagan What do you mean? If you want to comment my answer please do it in my answer's comments. And if you have a specific question please try to be accurate. _I don't follow you_ is not very informative. – Renaud Pacalet Feb 04 '22 at 07:13
  • @RaymundDalagan From that `xxd` output, the variable actually contains a newline character (the `0a` near the beginning of the second line of output); when you use the variable without double-quotes, it's treated as whitespace and therefore equivalent to a space character. Lesson: double-quote your variables to avoid confusion like this. As for why it's there, I don't know. I'd have to see the exact output from that `mysql` command, and maybe `mysql ... | grep 'Relay_Master_Log_File'` (in particular, does it output a blank line before the one with the filename on it?). – Gordon Davisson Feb 05 '22 at 03:51

1 Answers1

2

echo $var first expands variable var and then output the resulting words, separated by spaces, followed by a newline. So if there are leading or trailing spaces echo eats them because they are located before/after the first/last word. If you want to preserve the spaces double quote the variable expansion to produce one single word, including the spaces:

$ a=' node-bin.000002`
$ echo $a
node-bin.000002
$ echo "$a"
 node-bin.000002

If you want to remove the leading space from the variable value use a pattern substitution: a="${a# }". Example:

$ a=' node-bin.000002`
$ b="MASTER_LOG_FILE='$a'"
$ echo "$b"
MASTER_LOG_FILE=' node-bin.000002'
$ a="${a# }"
$ b="MASTER_LOG_FILE='$a'"
$ echo "$b"
MASTER_LOG_FILE='node-bin.000002'
Renaud Pacalet
  • 25,260
  • 3
  • 34
  • 51