1

I have an assignment to write a script that reads a number from the user and prints stars say if the number is 3 then it would print

***
**
*
**
***

I understand the idea of how to write it, but i try to make a variable that saves * in it but when i echo $variable it would just show all the folders/files in my current place. how do I just save the '*' as a string without it running the function?

1
  • 1
    Please show a sample of code that illustrates the problem. You can edit your question to include this. Don't forget to format it with the {} button on the editing toolbar. Commented Nov 9, 2015 at 22:55

2 Answers 2

7
varname='*'

Though you have to be careful with where you use it; since globbing occurs after variable expansion, if you expand it carelessly it'll do the glob operation at expand-time instead. Use echo "$varname" to print it (note the quotes).

2
  • Thanks alot I forgot the quotes when I print it that was my issue, kinda new to this thanks!
    – user142424
    Commented Nov 9, 2015 at 23:00
  • Note that variable=* works the same (it would be a different matter for array=(*) vs array=('*') though). The main point here is that parameter expansions must be quoted in POSIX shells. Commented Jan 6, 2020 at 12:27
4

Just quote the variable.

$ variable='*'
$ echo $variable
file1 file2 file3
$ echo "$variable"
*
1
  • 1
    Oh god I forgot I had to put the variable in quotes aswell when I do echo "$variable", thanks man!
    – user142424
    Commented Nov 9, 2015 at 22:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .