Skip to main content

All Questions

Tagged with
0 votes
1 answer
537 views

bash: string variable contains asterisk. how to use this variable for searching etc with grep, sed?

Have a nice day I have got text file (zz.txt): Chemical name 3-Aminopropane-1-sulphonic acid Synonym(s) -- Homotaurine * Tramiprosate -- Chemical name Common name and synonyms ... I have variable s=&...
Alex Den's user avatar
2 votes
1 answer
1k views

Why does variable expansion in commands not work in ZSH but it does in BASH?

I have set an environment variable and want to use it when calling a command, but no dice. The full command, without variable, works: nacho@WouldntYouLikeToKnow ~ % ls -la ~/Downloads ...
IMTheNachoMan's user avatar
0 votes
1 answer
2k views

How to save a result of any command into a variable?

Let's take this command sudo doveadm pw -s SHA512-CRYPT -p '$$!!!555;###..>^$$' How can redirect its output into a variable?
kesha933's user avatar
7 votes
2 answers
3k views

How to source some shell code not a file?

There is a description of a source command: source is a bash shell built-in command that executes the content of the file passed as argument, in the current shell. It has a synonym in . (period). For ...
t7e's user avatar
  • 323
37 votes
3 answers
6k views

Why is opening a file faster than reading variable content?

In a bash script I need various values from /proc/ files. Until now I have dozens of lines grepping the files directly like that: grep -oP '^MemFree: *\K[0-9]+' /proc/meminfo In an effort to make ...
dessert's user avatar
  • 1,715
61 votes
3 answers
15k views

How can I expand a quoted variable to nothing if it's empty?

Say I have a script doing: some-command "$var1" "$var2" ... And, in the event that var1 is empty, I'd rather that it be replaced with nothing instead of the empty string, so that the command executed ...
muru's user avatar
  • 76.3k
17 votes
3 answers
10k views

Loop over a string in zsh and Bash

I would like to convert this Bash loop: x="one two three" for i in ${x} do echo ${i} done in such a way to work with both Bash and zsh This solution works: x=( one two three ) for i in ${x[@]...
antonio's user avatar
  • 1,513
0 votes
1 answer
55 views

Is there a valid way of exporting a variable across different shells?

For some reasons, Im using ksh and zsh in my scripts and returning to -bash, I need to use already set variable values in the spawned shells, writing it to some temp file and using that didn't look ...
Keyshov Borate's user avatar
12 votes
6 answers
8k views

Serialize shell variable in bash or zsh

Is there any way to serialize a shell variable? Suppose I have a variable $VAR, and I want to be able to save it to a file or whatever, and then read it back later to get the same value back? Is ...
fwenom's user avatar
  • 223