Questions tagged [declare]

13 questions
49
votes
5 answers

What is "declare" in Bash?

After reading ilkkachu's answer to this question I learned on the existence of the declare (with argument -n) shell built in. help declare brings: Set variable values and attributes. Declare variables and give them attributes. If no NAMEs are…
user149572
8
votes
1 answer

Does `declare -a A` create an empty array `A` in Bash?

Does declare -a A create an empty array A in bash, or does it just set an attribute in case A is assigned to later? Consider this code: set -u declare -a A echo ${#A[*]} echo ${A[*]} A=() echo ${#A[*]} echo ${A[*]} A=(1 2) echo ${#A[*]} echo…
U. Windl
  • 1,095
  • 7
  • 21
5
votes
1 answer

Declare command and shell expansion

I stumbled by accident on the following bash behaviour, which is for me kind of unexpected. # The following works $ declare bar=Hello # Line 1 $ declare -p bar # Line 2 declare --…
Axel Krypton
  • 324
  • 2
  • 12
5
votes
2 answers

What is a variable attribute?

I aim to understand the general concept of "variable attributes" hoping it will help me understand what is declare in Bash. What is a variable attribute? Why would someone want to give an attribute to a variable? Why isn't just creating an variables…
user149572
4
votes
3 answers

Function to conditionally set a variable read-only

If I had a script which sets variables read-only to some odd values, and sets errexit because of other unsafe operations: #!/bin/bash set -e declare -r NOTIFY=$(case "$OS" in (macosx) echo macos_notify ;; (linux) echo linux_notify ;; (*) echo : ;;…
cat
  • 3,428
  • 4
  • 22
  • 50
3
votes
1 answer

Why does substituting eval with declare (for creating dynamic variables) result in an empty variable?

With bash >5, I'm trying to assign a different value to variables depending on the architecture specified in a variable. I use a function to do so. This works perfectly: # arguments: variable name to assign, value for mac arch, value for pi…
DeadBranch
  • 33
  • 6
3
votes
1 answer

How to read keyboard input and assign it to a local variable?

I have this very simple script: #!/bin/bash read local _test echo "_test: $_test" This is the output. $ ./jltest.sh sdfsdfs _test: I want the variable _test to be local only. Is this possible?
mrjayviper
  • 1,971
  • 3
  • 25
  • 41
1
vote
1 answer

correct way of storing external programm and making it executable?

I downloaded the CLI Client habash for the habit/routine gameification project habatica.com. In the fandom wiki for habash it is written that, I need to set environment variables. Additonally I want to make the program habash an ordinary…
1
vote
1 answer

Using cURL, jq, and declaration and for loop condition, I tried to download multiple files from a GitLab private repo, but it downloaded only one

I learned from the following sources: curl -O: Download a file with curl on Linux / Unix command line jq: How to urlencode data for curl command? Multiple files and curl -J: download pdf files from using curl Condition for loop: Shell: how to use 2…
Oo'-
  • 233
  • 1
  • 7
0
votes
1 answer

Bash create parameter named array within function

I'm attempting to write a function that writes arrays with a name that's passed in. Given the following bash function: function writeToArray { local name="$1" echo "$name" declare -a "$name" ${name[0]}="does this work?" …
Lee
  • 471
  • 3
  • 5
  • 15
0
votes
1 answer

Cannot Display Bash Functions within FZF Preview Window

How do I get the FZF Preview Window to Display Functions from my Current Bash Environment? I want to list my custom bash functions using FZF, and view the code of a selected function in the FZF Preview Window. However, it does not appear that the…
0
votes
1 answer

Reading variable from a txt file using bash

I'm new to bash. I'm trying to write a script that will read data from a text find and declare some variables. In the example below we read from a tab delimited file "ab.txt" that looks like this: a->AA b->BB Where -> denotes a tab. I'm reading…
Adi Ro
  • 101
  • 3
0
votes
1 answer

Parse 3rd entry in declare result

I have this: cd 31 /Users/alexamil/WebstormProjects/oresoftware/botch/botch-shell-overrides.sh the above style output, is given by this command: declare -F my_bash_func How can I grab just the file name from that result? something…
Alexander Mills
  • 9,330
  • 19
  • 95
  • 180