When GitHub Copilot came out, I joined the technology preview hoping it would help me be more productive at work. The result was extremely underwhelming. The code it produced was buggy and didn't meet any of the coding standards we use. It seemed more risk than reward, with the subtle bugs it would introduce. So, after a few hours of using it, I gave up.
Then, ChatGPT came out. It seemed like a similar hype cycle. But, I am genuinely interested in finding ways to be more productive. I'm also curious if one of these tools is going to take my job or become my boss. So, I messed around with it some. I thought it was better than Copilot but still not amazing. It seemed way too eager to give me wrong answers, apologize for them, and then give me the same wrong answers. So, like Copilot, I gave up after a few hours.
This morning, I read this article about one person's path to increased productivity with ChatGPT. The specific use case outlined for ChatGPT didn't strike me as particularly interesting. If I'm being totally honest, it seemed like most of the hello world examples I see. Cool that it helped this one person write some little script. But, not really going to move the needle for me getting my job done. For no apparent reason, though, this article lit a fairly large spark in me to try again to see if I could make use of ChatGPT.
Today at work, I spent a lot of time on this PR. We are trying to get NFS mounts working in a Fedora 36 VM running on a MacOS host. The meat of the work has been taking place in this file. It is a shell script to generate a Vagrantfile (a file which defines the configuration for a VM), which will create the VM and then do some provisioning of the machine. There are lots of little details to hold in one's head and many different cases all competing for my attention.
To add to the confusion, it uses bash parameter expansion heavily. For the life
of me, I can't remember what ${param:-1}
vs ${param-1}
vs ${param:+1}
etc., all mean. Every time I interact with parameter expansion, I need to visit
the
docs
to remind myself what each one of the expansions does, and I usually then end up
writing a little note to summarize everything.
Today, I decided to plug my questions into ChatGPT. I said:
Can you explain bash parameter expansion? Specifically,
${parameter:-word} ${parameter:-+word}
You'll notice right off the bat, I have a typo. The last example should be
${parameter:+word}
. ChatGPT didn't mind and gave me a very useful answer.
Bash parameter expansion is a powerful feature in shell scripting that allows
you to manipulate and transform variables or parameters in various ways. It
helps in enhancing the flexibility of scripts by providing ways to use default
values, substitutions, string manipulation, and more.
In your question, you've mentioned two types of parameter expansions:
${parameter:-word}
${parameter:+word}
Let's dive into each of these:
${parameter:-word} - This is called the default value parameter expansion.
It expands to the value of 'parameter' if it is set and not null; otherwise,
it expands to the value of 'word'. This is useful when you want to provide
a default value for a variable that may not be set or may be empty.
Example:
name="John"
greeting="Hello, ${name:-User}!"
echo $greeting
This will output "Hello, John!" since the 'name' variable is set to "John".
If 'name' were unset or empty, it would output "Hello, User!".
${parameter:+word} - This is called the alternate value parameter
expansion. It expands to the value of 'word' if 'parameter' is set and not
null; otherwise, it expands to nothing. This is useful when you want to
conditionally insert a value or perform an operation based on the existence of a variable.
Example:
name="John"
greeting="Hello${name:+, $name}!"
echo $greeting
This will output "Hello, John!" since the 'name' variable is set to "John". If
'name' were unset or empty, it would output "Hello!".
These parameter expansions are just a couple of examples of the many ways you
can manipulate variables in Bash scripts. They help in creating more robust and
versatile scripts by accounting for different situations and handling variables
more effectively.
From 1000 feet above, the docs and ChatGPT look to give the same answer. But some of the subtle wording in the ChatGPT response finally helped me hold in my head what each expansion does. What really did it for me was "this is called the default value parameter expansion" and "this is called the alternate parameter expansion". For whatever reason, "default" vs "alternate" caused things to click.
I was pretty impressed with ChatGPT at this point! I spent the rest of the day forgetting how expansion worked or running into a slightly different expansion and getting ChatGPT to help me. Like a perfect little helper, it held context all day long as I asked it questions.
The funniest part was, like a coworker helping, it even reminded me that I was dumb and it had already answered the question before:
In the given expression ${vagrant_dev_no_nfs_src:+1}, the parameter expansion
used is the alternate value parameter expansion, which we have covered earlier
as ${parameter:+word}.
So, while not earth-shattering and not doing my whole job for me, I actually found some real value in using ChatGPT to help me along. It won't take my job, but I definitely plan on having a tab with it running going forward.
P.S. If you're from the future and ChatGPT doesn't exist, send me a note so I can reminisce.
P.P.S. In a recent article, I was told by a friend that I could use more commas. I don't have the faintest idea where to put a comma when I write. But, I fed it through ChatGPT and it produced an article with at least 2x the number of commas, so from now on, all of my articles will have more comma baby.