2

Here's the script I am executing:

#!/bin/bash
...
function myprint() {
  cp=$1
  targetpermission=$2
  t1="RESOURCE"
  t2="Current Permission"
  t3="New Permission"
  printf "%s : %s ===> %s\n",$t1,$t2,$t3
}

myprint

Expected output:

RESOURCE : Current Permission ===> New Permission

Output I am getting:

    [abhyas_app01@localhost safeshell]$ ./test1.sh 
Permission,New : Permission ===> 
,RESOURCE,Current[abhyas_app01@localhost safeshell]$

Just what's going on? How do I get the expected output?

PS - echo is not an option, because eventually I am going to justify the strings using %-Ns where N is a calculated based on terminal width.

2
  • 6
    use printf without commas, i.e., printf "%s : %s ===> %s\n" $t1 $t2 $t3 Commented Sep 27, 2015 at 16:20
  • 4
    And quote your strings, since some of them contain spaces. Commented Sep 27, 2015 at 16:22

1 Answer 1

3

In the shell, printf is a command and command arguments are separated by spaces, not commas.

Use:

printf "%s : %s ===> %s\n" "$t1" "$t2" "$t3"

The double quotes are necessary too; otherwise the Current Permission and New Permission arguments will be split up and you'll get two lines of output from your single printf command.

I note that your function takes (at least) two arguments, but you do not show those being printed or otherwise used within the function. You may need to revisit that part of your logic.


With your current logic:

t1="RESOURCE"
t2="Current Permission"
t3="New Permission"
printf "%s : %s ===> %s\n",$t1,$t2,$t3

you really pass to printf:

printf "%s : %s ===> %s\n,RESOURCE,Current" "Permission,New" "Permission"

Note how the 'format string' argument includes all of $t1 and part of $t2. You only provide two arguments for the three %s formats, so the third one is left empty. The output is:

Permission,New : Permission ===> 
,RESOURCE,Current

with a space at the end of the first line and no newline at the end of the second 'line' (hence your prompt appears immediately after Current).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.