0

At my current job I am having to learn and interact a lot with Korn Shell

I was having a strange issue that I just learned is related to the differences of Bash and Ksh. Bellow you will see the difference in the output of this command. With bash I get a json, dictionary like output with Curly Braces in the beginning and end. With Ksh the Curly Braces are not present in the echo command and that breaks my code later on, as I'm expecting a json to be parsed.

This behavior is consistent to what I'm getting in my next file, where I can't parse the json.

token.sh

access_token_response=$(curl -i -k --tlsv1.2 -X POST https://api.cloud.com/oauth2/token -d client_id=id -d client_secret=secret -d grant_type=client_credentials)

echo $access_token_response

Outputs

$ bash token.sh
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   643    0   516  100   127  23329   5741 --:--:-- --:--:-- --:--:-- 30619
 {"issued_at":1654531548,"access_token":"eyJlbmMiO5yrqw","expires_in":1296000,"token_type":"Bearer"}
$ ksh token.sh
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   643    0   516  100   127  19549   4811 --:--:-- --:--:-- --:--:-- 24730
 "issued_at":1654531553 "access_token":"eyJlwMFlKCv7KjEw" "expires_in":1296000 "token_type":"Bearer"
[awsgfedsbtch@ip-204-56-124 shell]$

I would love to get a solution for this issue, otherwise I may have to write some strange hacks to get over it.

But what I really want is some sort of guide on how to know and deal with this differences. I don't see that much material about Ksh around and knowing how to bridge the gap between the 2 shells would help me a lot in the long run.

Thanks!

A Campos
  • 753
  • 3
  • 10
  • 31
  • 1
    I'm not sure why ksh would behave differently here... unless you set `IFS` to something unusual (which can cause all sorts of trouble). Anyway, the first thing to do is double-quote the variable (`echo "$access_token_response"`) because [unquoted variables get parsed in weird ways](https://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-shell-variable) in most shells (basically all except zsh). – Gordon Davisson Jun 06 '22 at 17:40
  • You should use `printf` instead of `echo`, and double-quote your variable expansions – Fravadona Jun 06 '22 at 18:54
  • Ok, that does fix the echo issue, but it doesn't fix it when passing the variable to another file as a parameter... – A Campos Jun 06 '22 at 19:45
  • `./otherfile.sh "$access_token_response"` should pass it verbatim – Fravadona Jun 06 '22 at 20:37
  • @ACampos Did you double-quote it when passing it as a parameter? Also, just for the record, had you changed `IFS`? – Gordon Davisson Jun 07 '22 at 02:16

0 Answers0