‘Write a shell script to take a string of comma separated words as an input, separate them with comma as the separator, trim them, and output them as a long string with ‘–flag ‘ preceeding each word.’
I gave ChatGPT the prompt above, and in 10 seconds, I had the perfect algorithm to solve this problem.
I had written that algorithm the week before, and it took more two hours to figure it out on my own, with lots of help from search engines and Stack Overflow. Also, ChatGPT’s solution made precisely the same choices that I had made, and it had better variable names.
What does this mean for my work?
Well, in the hours I spent figuring out this algorithm, I learnt a bunch of commands and how they are used. I learnt that ‘IFS’ stands for ‘Internal Field Separator’, and can be used to set any character as a delimiter. I learnt the syntax for for-loops and trimming whitespace characters in the Bash scripting language. Had I used ChatGPT, I would have merely copy-pasted the solution, verified that it worked and moved on.
On the one hand, I ‘wasted’ two hours by writing this algorithm on my own. On the other, I ‘invested’ those two hours in learning more about shell scripting works. Which of these is the truth? That depends on what I wish to do. If I wish to master shell scripting, outsourcing this work to ChatGPT is a missed opportunity. If I merely wanted to write a script and didn’t care about how it was done, ChatGPT is a wonderful assistant.
This leads us to a broader question in the face of smarter AI: what part of our work do we perform for its own sake, because it enriches us and sparks joy? Let us keep that part human. With what part of our work do we care more about the outcome rather than the process? Let us outsource that to AI.
Here is ChatGPT’s solution, in case you are interested:
#!/bin/bash
# Input string of comma separated words
input="$1"
# Convert input string to array
IFS=',' read -ra words <<< "$input"
# Trim each word and add '--flag ' before it
output=""
for word in "${words[@]}"; do
trimmed_word="$(echo -e "${word}" | sed -e 's/^[[:space:]]//' -e 's/[[:space:]]$//')"
output+="--flag ${trimmed_word} "
done
# Output the result
echo "$output"