1

I have this code:

echo `echo "$site_ids" | od -c`
for site_id in $site_ids; do
  wp_options_table="wp_$site_id_options"
  echo "$wp_options_table"
  echo `echo "$site_id" | od -c`
done 

it prints this output:

0000000 1 \r \n 2 \r \n 3 \r \n 4 \r \n 5 \r \n 0000017
wp_
0000000 1 \r \n 0000003
wp_
0000000 2 \r \n 0000003
wp_
0000000 3 \r \n 0000003
wp_
0000000 4 \r \n 0000003
wp_
0000000 5 \r \n 0000003

I can't figure out how to remove the \r\n so that the output is wp_1_options instead of wp. Or whatever other problem prevents the output from being wp_1_options

edit I got it working with :

echo "$site_ids" | while IFS=$'\r\n' read site_id
do
  wp_options_table="wp_${site_id}_options"
  echo "$wp_options_table"
done
user12341234
  • 1,137
  • 2
  • 10
  • 22
  • As a side note, using `$(...)` for command substitution rather than backticks has several advantages. – Enlico Feb 09 '20 at 20:00
  • @KamilCuk could you post the full code? I don't know bash well and don't know how to do while read – user12341234 Feb 09 '20 at 20:02
  • 1
    Also, there's never a good reason to `echo $(...)` instead of just running `...` directly. – Charles Duffy Feb 09 '20 at 20:04
  • 1
    BTW, it sounds like you want to run `wp_options_table="wp_${site_id}_options"`. Without the curly braces, there's no way for the shell to know that `_options` isn't part of the variable name too. – Charles Duffy Feb 09 '20 at 20:05
  • (BTW, the trailing newline isn't part of the value -- it's always, unconditionally added by `echo`; if you want to print a variable's value *without* adding a newline, use `printf '%s' "$your_variable"` instead). – Charles Duffy Feb 09 '20 at 20:09
  • @CharlesDuffy thanks ${} was part of the solution – user12341234 Feb 09 '20 at 20:09
  • BTW -- don't edit answers into questions. Use the "Add an Answer" button to add your own answer when a question is open. (Whereas in cases where they *aren't* open, we want folks to click the link to read the more-canonical version with its wider array of answers that have been voted on, edited, debated over and otherwise refined). – Charles Duffy Feb 09 '20 at 20:10
  • 1
    BTW, there's never a reason to include `$'\n'` in `IFS` if you aren't specifying the `-d` argument to `read`: As the default end-of-input delimiter is `$'\n'`, unless that default is explicitly changed, there can never be a `$'\n'` in the value `read` emits. – Charles Duffy Feb 09 '20 at 20:12
  • @CharlesDuffy, as regards `echo`, you wrote that the newline is _always, unconditionally added by `echo`_. What about `echo -n`? – Enlico Feb 10 '20 at 07:21
  • Funny thing, `echo -n` has no standard-mandated behavior; its sole standardized behavior is to make the standard no longer specify `echo`'s output. – Charles Duffy Feb 10 '20 at 14:39

0 Answers0