Questions tagged [variable-expansion]

Evaluating or expanding a variable to get its value. Depending on the language, a variable may be expanded one or more times.

246 questions
3
votes
2 answers

How to keep strings quoted in bash parameters

I have a Bash script which passes patterns and switches to grep. #!/bin/bash foo() { grep $@ *.txt } foo $@ And, of course, myscript SomeText works but myscript "Text1 Text2" does not. Is there a way to keep the quotes when passing arguments…
jackhab
  • 17,128
  • 37
  • 99
  • 136
3
votes
2 answers

Shell parameter expansion: how can I get the file name without the directory part?

I was writing a makefile and suppose I have the following; FILES = file1.py \ folder1/file2.py \ folder2/file3.py And I have the following for loop: -@for file in $(FILES); do \ echo $${file/folder1\/}; \ done The above…
user945216
  • 251
  • 3
  • 14
2
votes
0 answers

How to make properly designed survey objects?

I´m trying to use the srvyr package in R. The data is extracted from this link: Expenses: https://cdn.bancentral.gov.do/documents/estadisticas/encuesta-de-gastos-e-ingresos/documents/Cuadros_Gastos.xlsx?v=1689283267553 Income: …
almr27
  • 43
  • 4
2
votes
2 answers

bash - array expansion and function calls

SETUP: GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin21) Copyright (C) 2007 Free Software Foundation, Inc. TEST: check_this() { echo " FIRST: {$1} SECOND: {$2} THIRD: {$3}"; } foo=(1 2 3) expression="${foo[@]}" echo "via…
Frank-Rene Schäfer
  • 3,182
  • 27
  • 51
2
votes
1 answer

How to use a string with arguments to call a script as bash would do it when interprets the command line?

The desired outcome Is there a way to use a string that contains the arguments to call a script? str_params="this/one 'that one' and \"yet another\"" The function below prints feedback on the stdout on how the arguments were received: display_args…
rellampec
  • 698
  • 6
  • 22
2
votes
2 answers

Expansion of C preprocessor macros with integer arithmetic in a text file?

On my system, I have a /usr/include directory, with a curl/options.h file in it, which contains a preprocessor definition of CURLOT_FLAG_ALIAS. If I have the following test.txt: #include "curl/options.h" curlot flag alias is:…
sdbbs
  • 4,270
  • 5
  • 32
  • 87
2
votes
5 answers

How to write bash function to print and run command when the command has arguments with spaces or things to be expanded

In Bash scripts, I frequently find this pattern useful, where I first print the command I'm about to execute, then I execute the command: echo 'Running this cmd: ls -1 "$HOME/temp/some folder with spaces' ls -1 "$HOME/temp/some folder with…
Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265
2
votes
1 answer

Nested parameter expansion not working as expected

zsh says: ${name} The value, if any, of the parameter name is substituted. It then also says: If a ${...} type parameter expression or a $(...) type command substitution is used in place of name above, it is expanded first and the result is used…
2
votes
1 answer

Using variables as command arguments in zsh

I'm trying to write a function for zsh to find files with fzf and directly open them in my editor. The arguments for the call to fzf are stored in a variable like so: FZF_FILE_PREVIEW="--preview 'cat {}) 2> /dev/null | head -200'" the function…
namron
  • 75
  • 7
2
votes
1 answer

Parameter expansion for find command

Consider the code (the variable $i is there because it was in a loop, adding several conditions to the pattern, e.g. *.a and *.b, ... but to illustrate this problem only one wildcard pattern is enough): #!/bin/bash i="a" PATTERN="-name bar -or…
wmnorth
  • 247
  • 4
  • 9
2
votes
2 answers

How can I check if exists file with name according to "template" in the directory?

Given variable with name template , for example: template=*.txt. How can I check if files with name like this template exist in the current directory? For example, according to the value of the template above, I want to know if there is files…
AskMath
  • 415
  • 1
  • 4
  • 11
2
votes
2 answers

PowerShell using a variable in quotes within a command

I have this which works (triple double quotes are not a mistake; it works exactly like this)—concentrate on the Start-Process part: Start-Process powershell -Credential $cred -WorkingDirectory "$lettre_disque" -ArgumentList '-noprofile -command…
Rakha
  • 1,874
  • 3
  • 26
  • 60
2
votes
2 answers

zsh Looping through multiple parameters

In my old .bashrc, I had a short section as follows: PATH2ADD_SCRIPTBIN="/home/foo/bar/scriptbin" PATH2ADD_PYTHONSTUFF="/home/foo/bar/pythonprojects" PATH2ADDLIST="$PATH2ADD_SCRIPTBIN $PATH2ADD_PYTHONSTUFF" for PATH2ADD in $PATH2ADDLIST; do …
Trevor Sears
  • 459
  • 8
  • 24
2
votes
1 answer

How to escape special characters in a variable to provide commandline arguments in bash

I very often use find to search for files and symbols in a huge source tree. If I don't limit the directories and file types, it takes several minutes to search for a symbol in a file. (I already mounted the source tree on an SSD and that halved the…
NZD
  • 1,780
  • 2
  • 20
  • 29
2
votes
2 answers

Bash - How to force literal in subshell string?

I'd like to control variable expansion when executing a shell command using sudo bash -c. I know I can do it from a normal shell: bash$ export FOO=foo bash$ export BAR=bar bash$ echo "expand $FOO but not "'$BAR'"" expand foo but not $BAR How can I…
shj
  • 1,558
  • 17
  • 23