c**t 发帖数: 2744 | 1 有这么一段code:
#!/bin/ksh
func()
{
printf "%10s|%-10s\n" $x $x
return 0;
}
x=123
echo `func`
其output和
printf "%10s|%-10s" 123 123
不同. 区别在于第一个格式.
大家有什么意见, 赫赫 |
c*****t 发帖数: 1879 | 2 echo reformat output to single spaced. I often use it to strip spaces.
【在 c**t 的大作中提到】 : 有这么一段code: : #!/bin/ksh : func() : { : printf "%10s|%-10s\n" $x $x : return 0; : } : x=123 : echo `func` : 其output和
|
c**t 发帖数: 2744 | 3 如果将 echo 换成 printf 也不行呀
【在 c*****t 的大作中提到】 : echo reformat output to single spaced. I often use it to strip spaces.
|
c**t 发帖数: 2744 | 4 答案找到
y=`func`
echo $y
这时echo为什么不重新格式化?
【在 c**t 的大作中提到】 : 如果将 echo 换成 printf 也不行呀
|
c**t 发帖数: 2744 | 5
or
echo "`func`"
【在 c**t 的大作中提到】 : 有这么一段code: : #!/bin/ksh : func() : { : printf "%10s|%-10s\n" $x $x : return 0; : } : x=123 : echo `func` : 其output和
|
c*****t 发帖数: 1879 | 6
Actually, I take back what I said. It was NOT echo doing the formating.
Rather, when you call
echo `func`
the shell first get the results from func which can be 10 10
then, that thing is passed to echo by calling as
echo 10 10
Note that echo only see two arguments, not a formated string. So echo
just print the argument out with space in between. So, one way is to
assign the output to a temp variable in your case y. Then call echo
with double quotes around the variable
【在 c**t 的大作中提到】 : 答案找到 : y=`func` : echo $y : 这时echo为什么不重新格式化?
|
c**t 发帖数: 2744 | 7 U R Genius! I got it.
Thank you so much!!!!!!!!!
【在 c*****t 的大作中提到】 : : Actually, I take back what I said. It was NOT echo doing the formating. : Rather, when you call : echo `func` : the shell first get the results from func which can be 10 10 : then, that thing is passed to echo by calling as : echo 10 10 : Note that echo only see two arguments, not a formated string. So echo : just print the argument out with space in between. So, one way is to : assign the output to a temp variable in your case y. Then call echo
|